//生成小程序码
public function wx_code($path){
  $appid = 'wxf0000000000000000';
  $secret = '0a4a0000000000000000000000000';
  $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
  $access = file_get_contents($token_url);
  $access = json_decode($access,true);
  $access_token = $access['access_token'];
  
  // 配置参数
  $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$access_token}";
  $method = 'POST';
  $body = array('path'=>$path);
  $headers = array('Content-Type:'.'application/json');

  // 创建连接
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($curl, CURLOPT_FAILONERROR, false);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($body));
  curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
  curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);//严格校验

  // 发送请求
  $response = curl_exec($curl);
  curl_close($curl);
  $src = 'data:image/jpeg;base64,' . base64_encode($response);
  echo "<img src='{$src}'>";
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注