Maison > Questions et réponses > le corps du texte
public function GetOpenid()
{
//通过code获得openid
if (!isset($_GET['code'])){
//触发微信返回code码
$baseUrl = urlencode( 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$url = $this->_CreateOauthUrlForCode($baseUrl);
Header("Location: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$ openid = $this->getOpenidFromMp($code);
return $openid;
}
}
*/
fonction privée __CreateOauthUrlForOpenid($code)
{
$config = new WxPayConfig() ;
$urlObj["appid"] = $config->GetAppId();
$urlObj["secret"] = $config->GetAppSecret();
$urlObj["js_code"] = $ code;
$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;
}
fonction publique GetOpenidFromMp($code)
{
$url = $this->__CreateOauthUrlForOpenid($code);
//初始化curl
$ch = curl_init();
$curlVersion = curl_version();
$config = new WxPayConfig();
$ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
.$config- >GetMerchantId();
//设置超时
curl_setopt ($ ch, CURLOPT_TIMEOUT, $this-> curl_timeout); _setopt($ ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, TRUE) ; = "0.0.0.0" && $proxyPort != 0){
curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
}