首页
留言板
友情链接
Search
1
【javascript】JS-向当前url追加参数
2,327 阅读
2
【PHP】生成随机昵称
2,189 阅读
3
【PHP】判断一个字符串是否属于序列化后的数据
2,019 阅读
4
【css】html+css给文章页,做阅读全文
1,960 阅读
5
【PHP】 设计模式(23种)
1,898 阅读
默认分类
typecho
代码改变世界
mysql
Winform
go
设计模式
PHP
python
nginx
网络安全
文艺范
mac
Search
标签搜索
php
typecho
代码注释
mysql
redis
nginx
golang
docker
html
curl
linux
go
thinkphp
mamp
laravel
跨域
http
rsa
sql
酒
依然范儿特西
累计撰写
122
篇文章
累计收到
26
条评论
首页
栏目
默认分类
typecho
代码改变世界
mysql
Winform
go
设计模式
PHP
python
nginx
网络安全
文艺范
mac
页面
留言板
友情链接
搜索到
1
篇与
小程序码
的结果
2022-12-15
PHP解析小程序返回的buffer信息,并解析成图片
需要引入 GuzzleHttp 包composer require guzzlehttp/guzzle核心代码如下:<?php require __DIR__ . './vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; /** * get client * @return Client */ function httpGetClient() { static $client ; if (!$client) { $client = new Client(); } return $client; } //GET function httpGet($url) { $client = httpGetClient(); $options = [ 'verify' => false, ]; $response = $client->get($url,$options); return $response->getBody()->getContents(); } // POSTBODY function httpPostBody($url, $requestData=[], $header=[]) { $client = httpGetClient(); $options = [ 'headers' => $header, 'body' => json_encode($requestData,JSON_UNESCAPED_UNICODE ), 'verify' => false, ]; $response = $client->post($url, $options); if ($response->getStatusCode()==200) { return $response->getBody()->getContents(); }else{ return null; } } //postForm function httpPostForm($url, $requestData, $header) { $client = new Client(['verify' => false]); try { $response = $client->request('post', $url, ['form_params' => $requestData, 'headers' => $header]); $response->getStatusCode(); // 200 $response->getHeaderLine('content-type'); if ($response->getStatusCode()==200) { return [true,$response->getBody()->getContents()]; }else{ return [false,[]]; } } catch (GuzzleException $e) { } return [false,[]]; } function getAccessToken(){ $appid = ''; $secret = ''; //获取access_token $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret; $access_token_data = httpGet($access_token_url); $access_token_data = json_decode($access_token_data,true); return $access_token_data['access_token']; } function getQrcode($access_token){ $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$access_token; $requestData = [ 'path'=>'pages/order-status/index?loose_id=1', 'width'=>430, ]; $header = []; return httpPostBody($url,$requestData,$header); } $access_token = getAccessToken(); //$access_token = ""; $data = getQrcode($access_token); if(is_null(json_decode($data))){ //不是json数据, 有数据流,返回值为null $jpg = $data; //创建文件写入 $file = fopen("img/test.jpg",'w'); fwrite($file,$jpg); fclose($file); }else{ //不是数据流,则代表有错误发生 $data_array = json_decode($data,true); print_r($data_array); } ?> 如果提示你无法写入, 记得新建一个文件夹: img ,并给可写权限
2022年12月15日
24 阅读
0 评论
1 点赞