前言
本来这个问题是在网上可以找到很多的,但是担心以后会忘记了又要去找别人的,所以写个博客记录一下,这也是我之前看到别人写的借鉴的,忘记是谁了
代码
直接上代码,如下
<?php
$a = new WxQrcode();
$a->getEwm();
class WxQrcode{
static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";
static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";
//生成二维码
public function getEwm(){
$fqid = '参数';
$type = 1;//二维码类型 1永久 2临时
$ACCESS_TOKEN = '你的公众号的ACCESS_TOKEN';
$url = $this->getQrcodeurl($ACCESS_TOKEN,$fqid,$type);
echo $url;
}
protected function getQrcodeurl($ACCESS_TOKEN,$fqid,$type = 1){
$url = self::$qrcode_url.'access_token='.$ACCESS_TOKEN;
if($type == 1){
//生成永久二维码
$qrcode= '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": '.$fqid.'}}}';
}else{
//生成临时二维码
$qrcode = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": '.$fqid.'}}}';
}
$result = $this->http_post_data($url,$qrcode);
// print_r($result);
$oo = json_decode($result[1]);
if (empty($oo->ticket)){
return false;
}
if(!$oo->ticket){
$this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');
exit();
}
$url = self::$qrcode_get_url.'ticket='.$oo->ticket.'';
return $url;
}
protected function getToken($appid,$secret){
$ACCESS_TOKEN = file_get_contents(self::$token_url."appid=$appid&secret=$secret");
$ACCESS_TOKEN = json_decode($ACCESS_TOKEN);
$ACCESS_TOKEN = $ACCESS_TOKEN->access_token;
return $ACCESS_TOKEN;
}
protected function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
if (curl_errno($ch)) {
$this->ErrorLogger('curl falied. Error Info: '.curl_error($ch));
}
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
//下载二维码到服务器
protected function DownLoadQr($url,$filestring){
if($url == ""){
return false;
}
$filename = $filestring.rand(111,999).'.jpg';
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
$size=strlen($img);
$fp2=fopen('static/qrcode/'.$filename,"a");
if(fwrite($fp2,$img) === false){
$this->ErrorLogger('dolwload image falied. Error Info: 无法写入图片');
exit();
}
fclose($fp2);
return 'static/qrcode/'.$filename;
}
//错误日志
private function ErrorLogger($errMsg){
$logger = fopen('log.txt', 'a+');
fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."\r\n");
fclose($logger);
}
}
?>
这里的操作是生成二维码并且保存在本地,稍微对代码进行改动,不保存,直接输出返回的二维码链接
代码如下,复制过去就可以直接用
<?php
$a = new WxQrcode();
$a->getEwm();
class WxQrcode{
//构造方法
static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";
static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";
//生成二维码
public function getEwm(){
$p = '二维码中带的参数';
$type = 1;//二维码类型 1永久 2临时
$ACCESS_TOKEN = '你的公众号的ACCESS_TOKEN';//$this->getToken($APPid,$secret);
$url = $this->getQrcodeurl($ACCESS_TOKEN,$p,$type);
echo $url;
}
protected function getQrcodeurl($ACCESS_TOKEN,$p,$type){
$url = self::$qrcode_url.'access_token='.$ACCESS_TOKEN;
if($type == 1){
//生成永久二维码
$qrcode= '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": '.$p.'}}}';
}else{
//生成临时二维码
$qrcode = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": '.$p.'}}}';
}
$result = $this->http_post_data($url,$qrcode);
$oo = json_decode($result[1]);
if (empty($oo->ticket)){
return false;
}
if(!$oo->ticket){
echo "生成二维码失败";
exit();
}
$url = self::$qrcode_get_url.'ticket='.$oo->ticket.'';
return $url;
}
protected function getToken($appid,$secret){
$ACCESS_TOKEN = file_get_contents(self::$token_url."appid=$appid&secret=$secret");
$ACCESS_TOKEN = json_decode($ACCESS_TOKEN);
$ACCESS_TOKEN = $ACCESS_TOKEN->access_token;
return $ACCESS_TOKEN;
}
protected function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
}
嗯 改动很小,差不多就这样啦
其实在开头就已经调用了方法了
$a = new WxQrcode();
$a->getEwm();
如果是想直接测试使用的话 可以直接使用 你配置的本地域名/WxQrcode.php就可以访问了,前提是你把个文件命名为WxQrcode