首先生成 access_token
public function getAccessToken()
{
$url ="https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=".self::APPID."&secret=".self::SECRET;
$res = (new Curl())->getUrl($url);
return $res['access_token'];
}
生成二维码
public function getXcxCode(Request $request)
{
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" . $access_token;
$data['scene'] = 'h' . 2;
//小程序的详情页路径
$data['path'] = 'pages/detail/detail';
//二维码大小
$data['width'] = '500';
// dd(json_encode($data));
$res = (new Curl())->httpRequest($url,json_encode($data),"POST");
$path = 'upload/' . '2.png';
file_put_contents($path,$res);
$return['status_code'] = 2000;
$return['msg'] = 'ok';
$return['img'] = 'http://www.dome4.com/' . $path;
echo '<img src="'.$return['img'].'" />';
exit;
echo json_encode($return);
exit;
}
模拟请求
public function httpRequest($url, $data='', $method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '')
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
|