<?php
namespace app\api\controller;
class Wx extends Base{
const APPID = 'APPID';
const APP_SECRET = 'APP_SECRET';
public function get_openid(){
if (!isset($this->post['js_code'])) {
return json(['code' => 1, 'msg' => 'js_code必填']);
}
$res = $this->httpGet('https://api.weixin.qq.com/sns/jscode2session?appid='.self::APPID.'&secret='.self::APP_SECRET.'&js_code='.$this->post['js_code'].'&grant_type=authorization_code');
return json(['code' => '0000', 'msg' => '成功', 'data' => json_decode($res,true)]);
}
private function get_access_token() {
$access_token = cache('wx_access_token_'.self::APPID);
if ($access_token) {
return $access_token;
}
$res = $this->httpGet('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::APP_SECRET);
$data = json_decode($res, true);
if (isset($data['access_token'])) {
cache('wx_access_token_'.self::APPID, $data['access_token'], $data['expires_in']);
return $data['access_token'];
}
exit(__LINE__.':'.$res);
}
public function get_userinfo() {
if (!isset($this->post['openid'])) {
return json(['code' => 1, 'msg' => 'openid必填']);
}
$maxTry = 10;
// 微信的判斷是否關(guān)注的接口有時(shí)候會(huì)抽風(fēng),所以要多試幾次
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->get_access_token().'&openid='.$this->post['openid'].'&lang=zh_CN';
for ($i=0; $i < $maxTry; $i++) {
$tmpres = $this->httpGet($url);
$res = json_decode($tmpres, true);
if (isset($res['subscribe'])) {
return $res;
}
usleep(100*1000);
}
file_put_contents('lee.txt', '[error]:'.$tmpres, FILE_APPEND);
// 最終微信接口還是失敗了(暫時(shí)查不出什么原因。。),那就只能默認(rèn)返回的是已關(guān)注的結(jié)果了,代價(jià)是下次進(jìn)入時(shí),可能會(huì)提示需要關(guān)注
$data = [
'subscribe' => 1,
'openid' => $this->post['openid'],
'nickname' => '',
'sex' => 0,
'province' => '',
'city' => '',
'country' => '',
'headimgurl' => '',
];
return json(['code' => '0000', 'msg' => '成功', 'data' => $data]);
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 為保證第三方服務(wù)器與微信服務(wù)器之間數(shù)據(jù)傳輸?shù)陌踩?,所有微信接口采用https方式調(diào)用,必須使用下面2行代碼打開ssl安全校驗(yàn)。
// 如果在部署過程中代碼在此處驗(yàn)證失敗,請(qǐng)到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
@curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
微信php接口-備注
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 哎!苦于客戶一直要求,官方文檔看起來又蛋疼,磨了一個(gè)下午整理出一套試用Thinkphp5.1 調(diào)用微信掃一掃示例 ...
- wxml頁面 <view wx:if="{{config.tipsshow1}}" class='dialog-c...
- 小程序的坑 本文主要記錄下在寫微信小程序的時(shí)候遇到的一些問題,希望對(duì)遇到相同問題的朋友能有個(gè)幫助。 1. 使用ca...
- 1.公眾號(hào)獲取access_token接口 https://api.weixin.qq.com/cgi-bin/t...
- 在包名下單獨(dú)建一個(gè)包 wxapi =========== import android.graphics.Bit...