public function getAccessToken(){
$appId = env('WX_APP_ID');
$secretKey = env('WX_SECRET');
$url = "https://api.weixin.qq.com/cgi-bin/token";
$params = [
'grant_type' => "client_credential",
'appid' => $appId,
'secret' => $secretKey
];
$res = $this->request($url,$params,false);
return $res->access_token;
}
- 第二步:调用接口生成微信二维码(这里以接口B为例)
public function getCode($token,$sale_id)
{
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$token}";
$param = json_encode(array("scene"=>$sale_id,"width"=> 80));
$contents = $this->httpRequest( $url, $param,"POST");
$result=$this->data_uri($contents,'image/png');
return '<image src='.$result.'></image>';
}
public function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
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_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;
}
|