难点是请求https://api.weixin.qq.com/wxa/getwxacodeunlimit ,
的时候请求方式是raw,第二,由于返回的是图片的数据,因此,直接把返回的数据写进文件中就可以。
$appid = '';//
$secret='';
$sendurl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$return =$this->http_get($sendurl1) ;
$data=json_decode($return,true);
$pic=$this->http_postraw("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$data['access_token'],$user['id']) ;
$jpg = 'D://WWW/eschool/imgpublic/wxappshare_'.$data1['unionId'].'.jpg';
ob_end_clean(); //清空缓冲区
$fp = fopen($jpg,'w'); //写入图片
if(fwrite($fp,$pic))
{
fclose($fp);
}
/**
* POST 请求
* @param string $url
* @param array $param
*/
private function http_postraw($url,$param){
$curl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t\"page\":\"pages/index/index\",\n\t\"scene\":\"".$param."\",\n\t\"width\":\"280\"\n}\n",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return("cURL Error #:" . $err);
} else {
return($response) ;
}
}