微信41005(上传素材)

问题原因:以前用TP5,正常上传素材,最近换了TP6-php到7版本了,于是以前写好的代码现在报错!!!!

主要原因:PHP环境到了7.0以上了,CURL做了改变和一些禁用!

错误代码:41005

检查:用公众号提供的端口token等提交素材,成功!-等于端口tonken都正确

最后发现是curl的问题

网上大部分都是

curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);

但是会出现另外错误:

curl_setopt(): Disabling safe uploads is no longer supported

所以下来就用CURLFile方法了:

public function postFile($url,$data,$others=null){
        $ch                = curl_init($url);
        $cfile             = new \CURLFile($data);
        $others or $others = [];
        $others['media']   = $cfile;
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $others);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
  }

这里有一点要注意:

如果你传入的 $data=值=一定要是绝对路径的!这里不然还会保错!

你可以用一个网络上可以直接访问图片上传测试!

猜你喜欢

转载自blog.csdn.net/munchmills/article/details/127904589