Tags: 微信小程序
小程序二维码的接口返回的是二进制流,如果我们是想把小程序二维码图片存入OSS中,则不需要把二进制流转为图片保存再上传OSS,直接将获取的二进制流通过字符串上传的方式写入OSS的文件中。
一、选取合适的接口(此文选取接口B为例)
接口A: 适用于需要的码数量较少的业务场景
https://api.weixin.qq.com/wxa/getwxacode
接口B:适用于需要的码数量极多,或仅临时使用的业务场景
https://api.weixin.qq.com/wxa/getwxacodeunlimit
接口C:适用于需要的码数量较少的业务场景
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode
二、获取access_token
- 别忘了把access_token(两个小时内有效)存入缓存哦~
$url = "https://api.weixin.qq.com/cgi-bin/token";
$params = [
'grant_type' => 'client_credential',
'appid' => 'appid',
'secret' => 'secret',
];
$data = HttpClient::get($url, $params);
$access_token = $data['access_token'];
public static function get($url, $params) {
$url = $url . '?' . http_build_query($params);
return self::http($url, 'GET');
}
三、构建POST参数
$data = [
"scene" => $scene,
"width" => 430, //图片长宽
"page" => "", //小程序页面地址,不要带参数,需要的参数放入scene的值中
"line_color" => [
"r" => "0",
"g" => "0",
"b" => "0"
]
];
四、获取微信小程序二维码
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$imgData = HttpClient::post($url, $data);
public static function post($url, $params) {
$headers = array();
$body = http_build_query($params);
return self::http($url, 'POST', $body, $headers);
}
private static function http($url, $method, $postfields = NULL, $headers = array()){
$ci = curl_init();
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, 'X-YZ-Client 2.0.0 - PHP');
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
$response = curl_exec($ci);
$httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$httpInfo = curl_getinfo($ci);
curl_close($ci);
return $response;
}
五、把图片二进制流写入OSS
$filename = rand(1, 100) . time() . $scene . ".jpg";
$result = $this->ossClient->putObject($this->_bucket, $filename, $imgData);
if ($result) {
if (isset($result['info'])) {
$imgUrl = $this->host . $filename;
}
}
public function putObject($bucket, $object, $content, $options = NULL) {
$this->precheckCommon($bucket, $object, $options);
$options[self::OSS_CONTENT] = $content;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = $object;
if (!isset($options[self::OSS_LENGTH])) {
$options[self::OSS_CONTENT_LENGTH] = strlen($options[self::OSS_CONTENT]);
} else {
$options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH];
}
$is_check_md5 = $this->isCheckMD5($options);
if ($is_check_md5) {
$content_md5 = base64_encode(md5($content, true));
$options[self::OSS_CONTENT_MD5] = $content_md5;
}
if (!isset($options[self::OSS_CONTENT_TYPE])) {
$options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object);
}
$response = $this->auth($options);
if (isset($options[self::OSS_CALLBACK]) && !empty($options[self::OSS_CALLBACK])) {
$result = new CallbackResult($response);
} else {
$result = new PutSetDeleteResult($response);
}
return $result->getData();
}
ps:文章私人所有,转载请注明出处。
网友评论