Rumah > Soal Jawab > teks badan
Public Function GetOpenId ()
{
// 通过 Code 获得 OpenId
if (! Isset ($ _ get ['code'])) {
// 触发 微信 返回 code 码
$ baseUrl = urlencode (urlencode ( 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$url = $this->_CreateOauthUrlForCode($baseUrl);
Header("Lokasi: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET;
openid = $this->getOpenidFromMp($code); kembalikan $openid; } } */ fungsi peribadi __CreateOauthUrlForOpenid($🜎 = $Config)g() ;
$urlObj["appid"] = $config->GetAppId();
$urlObj["rahsia"] = $config->GetAppSecret();
$urlObj["js_code"] = $ kod;
$urlObj["grant_type"] = "authorization_code";
$bizString = $this->ToUrlParams($urlObj);
return "https://api.weixin.qq.com/sns/ jscode2session?".$bizString;
//return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
}
fungsi awam GetOpenidFromMp($code)
{
$url = $this->__CreateOauthUrlForOpenid($code);
//初始化curl$ch = curl_init();
Version
new WxPayConfig(); $ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
.$config- >GetMerchantId() setopt($ ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
t($PT_SHE,top CURLOPT_SSL,FALSE);curl_setopt($ch , CURLOPT_RETURNTRANSFER, BENAR);
$proxyHost = "0.0.0.0"; $proxyPort = 0; $config->GetProxy($proxyHost, $proxyPort !); = "0.0.0.0" && $proxyPort != 0){
curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
,
}结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出openid
$data = json_ecode);&
$tru, json_decode data = $data; $openid = $data['openid']; kembalikan $openid; }