发布于 4年前

在小程序里 用php curl 访问 imgSecCheck 报41005错误

想用到微信公众平台的图片识别系统,结果报错

{"errcode":41005,"errmsg":"media data missing hint: \[xScHza03164711\]"}"

代码如下:

<?php   
 class getImgSecCheck{  
function get\_data(){  
 $url = "https://api.weixin.qq.com/wxa/img\_sec\_check?access\_token=ACCESSTOKEN";  
 $post\_data = array("media"=>"@D:\\phpstudy\\PHPTutorial\\WWW\\addAll.png");  
 //初始化  
 $curl = curl\_init();  
 //设置抓取的url  
 curl\_setopt ( $curl, CURLOPT\_URL, $url );  
 curl\_setopt ( $curl, CURLOPT\_POST, 1 );  
 curl\_setopt ( $curl, CURLOPT\_RETURNTRANSFER, 1 );  
 curl\_setopt ( $curl, CURLOPT\_CONNECTTIMEOUT, 5 );  
 curl\_setopt ( $curl, CURLOPT\_SSL\_VERIFYPEER, FALSE );  
 curl\_setopt ( $curl, CURLOPT\_SSL\_VERIFYHOST, false );  
 curl\_setopt ( $curl, CURLOPT\_POSTFIELDS, $post\_data );  
 //执行命令  
 $data = curl\_exec($curl);  
 //关闭URL请求  
 var\_dump($data);  
 curl\_close($curl);  
 //显示获得的数据  
 }  
}  

 $getImgSecCheck = new getImgSecCheck();  
 echo $getImgSecCheck->get\_data();

这是因为php版本的不同,所导致的,在php5.5版本之前是可以用

$post\_data = array("media"=>"@D:\\phpstudy\\PHPTutorial\\WWW\\addAll.png");

来规定文件路径的,但是在php5.5之后就必须用到CURLFile 类;具体实现代码如下:

<?php   
 class getImgSecCheck{  
function get\_data(){  
 $url = "https://api.weixin.qq.com/wxa/img\_sec\_check?access\_token=ACCESSTOKEN";  
 $cfile = new CURLFile('D:\\phpstudy\\PHPTutorial\\WWW\\addAll.png','image/jpeg','test\_name');

 $post\_data = array('name' => 'Foo',"file"=>$cfile);  

 //初始化  
 $curl = curl\_init();  
 //设置抓取的url  
 curl\_setopt ( $curl, CURLOPT\_URL, $url );  
 curl\_setopt ( $curl, CURLOPT\_POST, 1 );  
 curl\_setopt ( $curl, CURLOPT\_RETURNTRANSFER, 1 );  
 curl\_setopt ( $curl, CURLOPT\_CONNECTTIMEOUT, 5 );  
 curl\_setopt ( $curl, CURLOPT\_SSL\_VERIFYPEER, FALSE );  
 curl\_setopt ( $curl, CURLOPT\_SSL\_VERIFYHOST, false );  
 curl\_setopt ( $curl, CURLOPT\_POSTFIELDS, $post\_data );  
 //执行命令  
 $data = curl\_exec($curl);  
 //关闭URL请求  
 var\_dump($data);  
 curl\_close($curl);  
 //显示获得的数据  
 }  
}  

 $getImgSecCheck = new getImgSecCheck();  
 echo $getImgSecCheck->get\_data();
©2020 edoou.com   京ICP备16001874号-3