2008/11/26

php serialize and serialize SimpleXMLElement

在PHP5中,序列化/反序列化simpleXMLElement对象时发生Warning: unserialize() [function.unserialize]: Node no longer exists in...的错误。

查了一下,http://bugs.php.net/bug.php?id=46143&edit=1,里面有段描述:

AFAIK, you cannot serialize a SimpleXMLElement, because it's a resource wrapped inside an object and it would be impossible de restore thatresource upon unserialize()-ation. That's why you receive an error: theunserialized object doesn't know where's the original document anymore.Store it in text form instead (asXML()).
说是SimpleXMLElement这个对象内部是包装的资源,即php中的resource类型,而resource是不可以序列化的。

上述文字提供了一个解决方案,保存其xml字符串,然后再通过simplexml_load_file/simplexml_load_string恢复SimpleXMLElement对象即可,xml字符串可以通过SimpleXMLElement->asXML()得到。

2008/11/24

nusoap与php5内置soap模块类名冲突解决办法

Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in...

出现上述错误,原因是与php5内置soap模块类名冲突。

解决办法:
1.关闭php5的soap扩展。windows关掉extension=php_soap.dll即可,Ubuntu下apt-get install安装的没有此选项,只好重新编译。

2.(推荐此法)修改类名即可。将soapclient()改为nusoap_client(),soap_server()改为nusoap_server()。这样就永远不会发生冲突问题了。nusoap代码中有如下片段:

/**
* Backward compatibility
*/
class soap_server extends
nusoap_server {
}
/**
* For backwards compatiblity, define
soapclient unless the PHP SOAP extension is loaded.
*/
class soapclient
extends nusoap_client {
}

3.php5改为php4。

2008/11/22

为什么他们可以这么High?

《天津邮政职工搞笑舞蹈 请自备避雷针。》

邮政局都可以这么Hight?很是纳闷,记得以前看过华为的嘻唰唰,还可以,这个实在太雷人。。。不知真的假的?



《TA 们放肆摇晃的青春尾巴》

传说来自网易的。

2008/11/11

SimpleXMLElement解析报错解决方案:剔除字符串中错误字符

实在不愿意写关于PHP乱码方面的东西了,今天再次遇到了一个很郁闷的问题,字符串中含有错误的字码(内码)。

报错:
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]:
input conversion failed due to input error, bytes 0xC2 0x2E 0x20 0x20 in
E:\www\alsobuy.php on line 47
PHP在解析XML文档时发生错误,这个问题很常见,主要是由于在xml文档中含有头部声明编码以外的字符,而php严格按照头部声明的编码来解析xml文档。通常都是GB2312编码发生此情况,我一般是使用正则来修改文档头部编码声明来避免。如下:
$xml = preg_replace("/(^<\?xml.*encoding.*)GB2312(.*\?>)/iU" ,
'${1}GBK${2}' , $xml );
一般将其修改为GBK即可,GB2312包含的汉字实在太少了。

今天却不好使,纳闷了,后来终于发现,该XML文档中含有一个错误字符,根本无法解析。

而在使用正则等处理字符串时,里面含有乱码一般不会报错,估计SimpleXmlElement()是堆栈来完成xml文档解析的,遇到错误字符就挂了。

解决方案:根据具体编码,确定字符内码的范围,遍历一遍,剔除错误的字符。

下面是根据GBK编码范围来剔除的,效果很好。

代码:
<?php
//含有两个错误的字符
$str = "我\x97\x7f鎔ㄅㄈ是德\x82\x09文";

$len = strlen($str);//长度
$new_str = "";

for($i=0; $i <= $len-1 ;$i++) { $s_hex = ord($str[$i]); if( $s_hex <= 0x7f && $s_hex >=0x00 )
{
//ACSII
$new_str .= $str[$i];
}
else if( $s_hex >= 0x81 && $s_hex <=0xfe )
{
//双字节
if( $i == $len-1 ) break;
$i++;
$s_hex = ord($str[$i]);
if( $s_hex >= 0x40 && $s_hex <= 0xfe && $s_hex != 0x7f)
{
$new_str .= $str[$i-1];
$new_str .= $str[$i];
}
}
}
echo $str.'<br/>';
echo $new_str.'<br/>';
?>



效果图:


方便看代码:

飓风营救(Taken),十年编程无师自通

晚上看了一部电影,《飓风营救》,有点这个杀手不太冷的味道。整部片子都不错,前面节奏很慢,中间非常紧凑。结尾我以为她女儿会有比较大的遗留症状,结果经历了那些,回来时就好像没啥事发生一样,不解。推荐有空看看。

还看了一篇很早的文章,《10年编程无师自通》,君子固本,不能太急,十年磨一剑,尤其对于像我这样的新手呵。

2008/11/10

为何keso的可以访问,偶们的就不行?(已解决)

Keso的Blogger地址:http://log.keso.cn/,可以访问。

我使用自己的域名托管指向到ghs.google.com就不可以?GFW对keso特殊监控?

--update 19:09

keso回了邮件。解决办法:找到未被封掉的ghs.google.com的ip,然后把自己域名A记录指向过去即可。

2008/11/08

利用BlackBerry架设Now SMS/MMS Gateway服务器失败

最近想玩点彩信的东西,使用电脑来处理发送彩信,网上找了一下,Now SMS/MMS GateWay,使用的我的BB8820作GSM/GPRS Modem,结果出现错误,在网上Now SMS/MMS官网上找到一些文档,结果是Now SMS/MMS GateWay也许不支持BlackBerry Modem。

郁闷,不过后来看到一个Nokia的MMS开发包(http://www.blogjava.net/amplifier/archive/2008/06/17/208620.html),也许可以试试,再研究。

贴个错误的图,将bb作为标准Modem和“Modem does not support SMS --”的错误。

参考:
Use BlackBerry As Modem For Laptop!
http://www.blackberryforums.com/blackberry-guides/2019-user-howto-use-blackberry-modem-laptop.html


2008/11/06

我的BB8820终于解决GoogleMap的问题了,不再假死

前一阵子,在BB上装了一个Gmail,都说其好使,可我打开后,进度条总是在1/3的地方卡死,连打开都没有成功过,很是郁闷,后在网上看到需要修改APN为CMNET。到设置里看,我的是CMWAP,恩,改成CMNET,啊,果然好使了,又说APN置空即可,会自动选择,于是便置空了,恩,很好使。


最近装了google map,郁闷了,又出现gmail刚开始的情况,哎,看到http://fisio.cn/blackberry-china-google-maps-232.html#comment-5427又是很兴奋,再次安装googlemap,后来,在APN里填上CMNET,一切ok,这个世界清净了。下班路上试了试,很好很强大!


还有opera mini,好多人都说是黑莓上最好使的,我还是觉得官方浏览器好使一些。PC版的opera还是很棒的。


科技以人为本,如果手机、电脑这电子产品这些都做的像开门关门一样简单,这个世界将会是多么美好啊。


Goolge Map截图一张:





----

2008/11/05

Apache2.2开启Gzip和Expires来提升网页浏览速度,yslow评分A(96)

在Apache的配置文件中找到下面两行,将注释#去掉,重启。

LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so

在你网站的目录下新建.htaccess,添加如下内容:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
</IfModule>

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
</IfModule>

解释一下。ExpiresByType 是通过MIME类型来设置具体文件的缓存时间,A表示访问,A后面的数字表示访问后的缓存时间。AddOutputFilterByType表示根据后面的MIME类型来压缩文件,这里对css,html,gif,jpeg,png,JavaScript等进行gzip压缩。更多选项请参考apache手册哦。

关闭ETag。Etag的问题在于,它是根据可以辨别网站所在的服务器的具有唯一性的属性来生成的。当浏览器从一台服务器上获得页面内容后到另外一台服务器上进行验证时ETag就会不匹配,这种情况对于使用服务器组和处理请求的网站来说是非常常见的。在配置文件中写入下面一行即可:

FileETag none

如果是多服务器负载均衡,可以设置为FileETag MTime Size,apache默认设置为FileETag INode MTime Size,去掉INode

经过上面的设置,开启YSlow,评分A(96)。



--

傲游“全新设计的页面内查找工具条”令我很不舒服

“全新设计的页面内查找工具条”,每次都要使用鼠标才能定位到查找关键字对话框中,或者按两次“Ctrl+F5”,对于页面开发人员来说实在太郁闷了。

或者有什么快捷方式我不知道?

今天把新版的卸载了,安装以前版本,mx_2.1.0.2082cn.exe

2008/11/03

php生成扭曲变形的验证码

效果:
原理:通过数学函数对图像的点重新分布,得到扭曲效果。首先生成一副正常的图片,然后遍历每个点,取得其rgb色和坐标,通过数学函数变换点的坐标值,然后在写入到新的图像中,这样即得到扭曲的图形。要得到更复杂的图形,可以把每个字符当做一副图片,使用不同的字体,变形后再写入新的图像,就可以得到像google那样扭扭曲曲的验证码了。
 
line18:中"ROOT.'/app/vendors/MSYHBD.TTF'"为字体文件路径,修改指向某个字体文件即可。
 
代码如下,在项目中可以使用,单独拿出来未测试:
 
01 <?php
02 /**
03 * 取得验证码图像
04 *
05 * @param string $string 字符
06 * @param int $im_x
07 * @param int $im_y
08 */
09 function getAuthImage( $string , $im_x = 120 , $im_y = 60) {
10         ob_start();
11         $im_x = 120;
12         $im_y = 60;
13
14         $im = imagecreatetruecolor ($im_x, $im_y);
15         imagefill($im, 0, 0, imagecolorallocate($im,255,255,255) );
16
17         $stringColor = imagecolorallocate($im, 17, 158, 20);
18         imagettftext ($im, 24, rand(-6 , 6), $im_x*0.1, $im_y*0.7, $stringColor, ROOT.'/app/vendors/MSYHBD.TTF', $string);
19        
20         //扭曲,变形
21         $distortion_im = imagecreatetruecolor ($im_x*1.5 , $im_y);       
22         imagefill($distortion_im, 0, 0, imagecolorallocate($distortion_im,255,255,255) );
23         for ( $i=0; $i<$im_x; $i++) {
24             for ( $j=0; $j<$im_y; $j++) {
25                 $rgb = imagecolorat($im, $i , $j);
26                 if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im) && (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
27                     imagesetpixel ($distortion_im, (int)($i+20+sin($j/$im_y*2*M_PI-M_PI*0.4)*8) , $j , $rgb);
28                 }
29             }
30         }
31        
32         //imageline
33         for($i=0; $i <= 1; $i++) {
34             $linecolor = imagecolorallocate($distortion_im, 17, 158, 20);
35             $lefty = rand(1, $im_y-1);
36             $righty = rand(1, $im_y-1);
37             imageline($distortion_im, 0, $lefty, imagesx($distortion_im), $righty, $linecolor);
38         }
39         //pixel
40         for($i=0; $i <= 64; $i++) {
41             $pointcolor = imagecolorallocate($distortion_im, 17, 158, 20);
42             imagesetpixel($distortion_im, rand(0, imagesx($distortion_im)), rand(0, imagesy($distortion_im)), $pointcolor);
43         }
44         //边框
45         //imagerectangle($distortion_im, 0, 0, imagesx($distortion_im)-1, imagesy($distortion_im)-1, imagecolorallocate($distortion_im, 17, 158, 20) );
46
47         ob_clean();
48         header('Content-type: image/jpeg');
49         imagejpeg ($distortion_im);
50         imagedestroy($im);
51         imagedestroy($distortion_im);
52 }
53 ?>
---