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

想用到微信公众平台的图片识别系统,结果报错{"errcode":41005,"errmsg":"media data missing hint: [xScHza03164711]"}"

代码如下:

 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 类;具体实现代码如下:

 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();

 

 

 

你可能感兴趣的:(phpcurl)