php5.2升级5.6语法以及问题汇总

  1. 类静态方法的调用规范化
    php5.2
    class类中public方法可直接用 类名::方法名() 调用
    php5.6 会报错

    Message: Non-static method unify_model::getMenu() should not be called statically, assuming $this from incompatible context
    
  2. 一些函数执行失败
    a. json_decode 处理特殊字符失败
    5.6后版本的PHP,JSON处理数据时,遇到非UTF-8特殊字符,会直接返回false,之前则是会将特殊字符转化为NULL。这样会导致JSON无法解压/压缩数据成功。

    b. mcrypt_encrypt 加密失败
    当参数密钥key长度大于8位时,函数返回false,PHP 5.6版本后,不再接受无效长度的 key and iv 参数
    如果参数密钥key长度大于8位,mcrypt_decrypt() 函数会产生警告并且返回 false,导致加密失败。

    c. curl模拟post上传文件不能通过@文件来进行上传了
    5.5之前都可以通过@/tmp/test.jpg这样的方式直接上传,5.5之后,CURLOPT_SAFE_UPLOAD默认为true,不能通过@文件上传了。
    解决方案: 将curl句柄CURLOPT_SAFE_UPLOAD 设置为false。

    $ch = curl_init();
    $data = array(
    	‘i’ => ‘@/Users/zmx/Desktop/demo.jpg’,
    	‘q’ => 80,
    	‘t’ =>160×160,
    );
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); //here
    curl_setopt($ch, CURLOPT_URL,  $upload_api);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    

    或者改用 CURLFile 类 http://php.net/manual/en/class.curlfile.php

    工作中curl POST上传文件示例:

    $data = array(
    //.";type=".$filedata['type'].";filename=".$filedata['name']
    // 	'filedata'	=>	'@'.realpath($params['data']['tmp_name'])				// 5.5之前的写法
    	'filedata'	=>	new CURLFile( realpath($params['data']['tmp_name']) )	// 5.5之后推荐的写法
    );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt( $ch, CURLOPT_HEADER, false);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    //添加如下head头就可传输大于1024字节请求
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt( $ch, CURLOPT_TIMEOUT, 500 );
    $result = curl_exec($ch);
    curl_close($ch);
    

    d. mysql_connect 连接数据库失败
    PHP5.5以后,mysql扩展已经被移除,现在只能使用mysqlipdo连接mysql数据库。
    解决方案: 将mysql扩展换为mysqli或者pdo以兼容新版PHP。

  3. php高版本废弃$HTTP_RAW_POST_DATA

    在高版本 php 的发版说明中都有 $HTTP_RAW_POST_DATA 即将(已经)取消,请改用从 php://input 中读取 的声明

    Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set ‘always_populate_raw_post_data’ to ‘-1’ in php.ini and use the php://input stream instead. in Unknown on line 0

    解决方案:
    修改php.ini中 always_populate_raw_post_data = -1
    $HTTP_RAW_POST_DATA详解见我另外一篇博文:

    https://blog.csdn.net/Fzqx_/article/details/88397178

  4. loading…

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值