thinkphp5获取小程序码


基于获取小程序码保存到手机的需求,使用tp5进行后台的处理,下面是获取小程序码的方法:

//二维码生成
public function code()
{
$user_id = $this->uid;
$user = Users::where("user_id = $user_id")->find();
if($user['code_img']){
return $this->success($user['code_img']);
}else{
//获取token
$appID = "XXX";
$appSecret = "XXXXXXXXX";
$tokenUrl= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$appID ."&secret=".$appSecret;
$getArr=array();
$tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
$access_token=$tokenArr->access_token;
//生成二维码
$path="pages/index/index?parent_id=".$user_id;
$width=500;
$data = [
'path'=>$path,
"width"=>$width,
'auto_color'=>false,
//'line_color'=>$line_color,
];
$post_data= json_encode($data,true);
$url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token;
$result=$this->api_notice_increment($url,$post_data);
if ($result){
$img = "code/".$user_id."code.jpg";
$url = ROOT_PATH.'public/uploads/img/'.$img;
if(file_put_contents($url, $result)){
$code = [
'code_img'=>$img,
];
Users::where("user_id = $user_id")->update($code);
return $this->success($img);
};
}
}
return $this->error("获取二维码失败");
}

function send_post($url, $post_data,$method='POST') {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $method, //or GET
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}

function api_notice_increment($url,$data)
{
$curl = curl_init();
$a = strlen($data);
$header = array("Content-Type: application/json; charset=utf-8","Content-Length: $a");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
curl_close($curl);
return $res;

}

以上就是获取小程序码的方法

优质内容筛选与推荐>>
1、ASP.NET 2.0 正式版中callback的一些变化+使用示例
2、北大才女长张泉灵语录
3、IO多路复用
4、iOS_自定义毛玻璃效果
5、MYSQL提示 错误代码:1248 Every derived table must have its own alias


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号