扣丁学堂PHP在线学习之使用微信接口实现二维码的生成

2018-02-02 10:52:17 609浏览

  今天扣丁学堂小编给大家带来一个实用比较强的编程入门教程,现在微信的普及度相当的高,而二维码对于渠道的营销推广的用处是非常大,现二维码分为两种:临时二维码、永久二维码,下面就来研究学习一下吧,现在可以通过微信接口实现二维码的生成。


/**

*CreatedbyPhpStorm.

*User:bin

*Date:15-1-16

*Time:上午9:48

*/

namespaceHomeCommon;

//微信处理类

set_time_limit(30);

classWeixin{

//构造方法

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?";

//生成二维码

publicfunctiongetEwm($wechatid,$fqid,$type=1){

$wechat=M('Member_public')->where(array('id'=>$wechatid))->find();

$appid=$wechat['appid'];

$secret=$wechat['secret'];

$ACCESS_TOKEN=$this->getToken($appid,$secret);

$url=$this->getQrcodeurl($ACCESS_TOKEN,$fqid,1);

returnDownLoadQr($url,time());

}

protectedfunctiongetQrcodeurl($ACCESS_TOKEN,$fqid,$type=1){

$url=self::$qrcode_url.'access_token='.$ACCESS_TOKEN;

if($type==1){

//生成永久二维码

$qrcode='{"action_name":"QR_LIMIT_SCENE","action_info":{"scene":{"scene_id":'.$fqid.'}}}';

}else{

//生成临时二维码

$qrcode='{"expire_seconds":1800,"action_name":"QR_SCENE","action_info":{"scene":{"scene_id":'.$fqid.'}}}';

}

$result=$this->http_post_data($url,$qrcode);

$oo=json_decode($result[1]);

if(!$oo->ticket){

$this->ErrorLogger('getQrcodeurlfalied.ErrorInfo:getQrcodeurlgetfailed');

exit();

}

$url=self::$qrcode_get_url.'ticket='.$oo->ticket.'';

return$url;

}

protectedfunctiongetToken($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;

}

protectedfunctionhttp_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('curlfalied.ErrorInfo:'.curl_error($ch));

}

$return_content=ob_get_contents();

ob_end_clean();

$return_code=curl_getinfo($ch,CURLINFO_HTTP_CODE);

returnarray($return_code,$return_content);

}

//下载二维码到服务器

protectedfunctionDownLoadQr($url,$filestring){

if($url==""){

returnfalse;

}

$filename=$filestring.'.jpg';

ob_start();

readfile($url);

$img=ob_get_contents();

ob_end_clean();

$size=strlen($img);

$fp2=fopen('./Uploads/qrcode/'.$filename,"a");

if(fwrite($fp2,$img)===false){

$this->ErrorLogger('dolwloadimagefalied.ErrorInfo:无法写入图片');

exit();

}

fclose($fp2);

return'./Uploads/qrcode/'.$filename;

}

privatefunctionErrorLogger($errMsg){

$logger=fopen('./ErrorLog.txt','a+');

fwrite($logger,date('Y-m-dH:i:s')."ErrorInfo:".$errMsg."rn");

}

}


以上就是这次编程入门教程中关于PHP微信开发之使用微信接口实现二维码生成的PHP学习,,最后想要了解更多关于PHP开发发展前景趋势,请关注扣丁学堂官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的PHP培训视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的一套PHP视频教程课程,让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP开发工程师技术交流群:374332265。



扣丁学堂微信公众号



关注微信公众号获取更多学习资料



查看更多关于“php培训资讯的相关文章>>

标签: PHP培训 PHP视频教程 PHP从入门到精通 PHP学习路线图 PHP开发工程师

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号