新手提問,在按照微信介面的頁面可以得到正確的資料,但是寫成PHP的程式碼卻無法得到正確的資料
還請有沒有大神可以協助指導
我是參考書本上的資料,一下是weixin.class.php的代碼,APPID及appsecret就不表達了
class class_weixin
{
var $appid = APPID;
var $appsecret = APPSECRET;
//建構式,獲取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//2. 快取形式
if (isset($_SERVER['HTTP_APPNAME'])){ //SAE環境,需要開通memcache
$mem = memcache_init();
}else { //本地環境,需已安裝memcache
$mem = new Memcache;
$mem->connect('localhost', 11211) or die ("Could not connect");
}
$this->access_token = $mem->get($this->appid);
if (!isset($this->access_token) || empty($this->access_token)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$mem->set($this->appid, $this->access_token, 0, 3600);
}
}
//生成OAuth2的URL
public function oauth2_authorize($redirect_url, $scope, $state = NULL)
{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
return $url;
}
//生成OAuth2的Access Token
public function oauth2_access_token($code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
$res = $this->http_request($url);
return json_decode($res, true);
}
//獲取用戶基本資訊(OAuth2 授權的 Access Token 獲取 未關注用戶,Access Token為臨時獲取)
public function oauth2_get_user_info($access_token, $openid)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
//獲取用戶基本資訊
public function get_user_info($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
//HTTP請求(支持HTTP/HTTPS,支持GET/POST)
protected function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
下面是index.php的頁面代碼
<?php
echo "hello 111";
require_once('weixin.class.php');
$weixin = new class_weixin();
if (!isset($_GET["code"])){
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_base", "123");
Header("Location: $jumpurl");
echo "hello yes";
}else{
echo "hello err1";
$access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
$userinfo = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']);
echo "hello err";
}
echo "hello end2";
?>
再下面的是html的代碼,放在index.php中就不顯示了,上面的“hello”等echo是為了確認是否有讀取到index設定的
但是一直沒有辦法獲得正確的值
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/77155.html
標籤:微信開發
上一篇:無力吐槽的廢醬的幾個問題
