php使用curl访问https返回无结果的问题

时间:2022-08-06 04:49:49

最近在做一个微信自动登录,发起验证以后回调页面获取openid时 curl函数返回空。

$appid = "appid appid "; 

$secret = "secret "; 

$code = $_GET["code"]; 

$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';

//header("location:$get_token_url");

echo $get_token_url."<BR>";

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$get_token_url); 

curl_setopt($ch,CURLOPT_HEADER,0); 

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1 ); 

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); 

$res = curl_exec($ch); 

echo "echo:".json_encode($res)."<br>";     //显示false

echo curl_multi_getcontent($ch)."<br>";   //空

curl_close($ch);                     

$json_obj = json_decode($res,true); 

//根据openid和access_token查询用户信息 

$access_token = $json_obj['access_token']; 

$openid = $json_obj['openid']; 

var_dump($res);                                                     //显示obj(false)

echo $openid;

结果curl无返回值,最终百度到一片文章  http://yanda.net.cn/articles/453  文章中提到

php使用curl访问https返回无结果的问题

改成如下问题解决

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$get_token_url); ​

curl_setopt($ch,CURLOPT_HEADER,0); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1 ); 

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); 

$res = curl_exec($ch); 

var_dump($res);  //有返回值