微信获取sessionkey 前端用wx.request发起请求,服务器php curl_init() 执行失败,后台命令行执行脚本正常获得session_key
前端
success: function(res) {
var code = res.code;
if (code) {
console.log('获取用户登录凭证' + code);
wx.request({
url: 'https://www.xxxxx.club/byland/userlogin/onlogin2.php',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'POST',
data: {
code: code
},
.......................................................................................................................................................................
服务器 onlogin2.php
if(isset($_GET['code'])){
hq_AccessToken($_GET['code'],$appId,$appSecret);
echo $_GET['code'];
}
function hq_AccessToken($code,$appId,$appSecret){
$requestParams = [
¦ 'appid' => $appId,
¦ 'secret' => $appSecret,
¦ 'js_code' => $code,
¦ 'grant_type' => 'authorization_code'
];
$url = 'https://api.weixin.qq.com/sns/jscode2session?' . http_build_query($requestParams);
$res=httpRequset($url);
print_r($res);
}
function httpRequset($url,$data=false){
$ch=curl_init();//-----------------执行到这里就不再运行
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
if($data){
¦ curl_setopt($ch,CURLOPT_POST,1);
¦ curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$res=curl_exec($ch);
curl_close($ch);
return $res;
}
.......................................................................................................................................................................
手动填入code ,在服务器终端执行 $php ./onlogin2.php 能输出
朋友,我解决了,重启服务器,服务器apt install php5-curl 后可以使用curl,但是php-fpm 没有执行权,重启就解决了。重启真是个好东西
我也是同样的问题.code换取openid返回null.即data:null.没有看到别人以前出现这类问题阿!!