你瞅啥呢

2021/01/16 在线上调试时,开发工具获取得到openid,真机调试获取不到openid

2021/01/16
代码如下:

复制代码
if ($a == 'get_openid_card') {

$input = file_get_contents("php://input");

$obj = json_decode($input, true);

$code = checkInput($obj['code']);

$appId = checkInput($obj['appId']);

$secret = $serverConfig->getCardWxSecret();

$AccessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appId . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";

$result = json_decode(file_get_contents($AccessTokenUrl));

if ($result) {
echo (JSON(array('data' => $result, 'status' => '06626492603032')));
exit();
} else {
echo (JSON(array('data' => $result, 'status' => '15115415254862')));
exit();
}
}
复制代码

我的情况:如题所述,参数正确,链接正确,问题出在获取链接内容的方法上。
在真机,$result = json_decode(file_get_contents($AccessTokenUrl)) 获取到的结果为空,也就是null;
改成$result = file_get_contents($AccessTokenUrl) 获取到为false,说明file_get_contents读取链接内容失败,在网上搜索了资料,模糊觉得是没有开启openssl的缘故,需要一个叫php_openssl.dll的东西,
说是“只要是使用函数file_get_contents访问 https 的网站都要开启”,
我没有去实践,而是采用了curl方法获取。
结果是喜人的。
完整代码如下:

复制代码
if ($a == 'get_openid_card') {

$input = file_get_contents("php://input");

$obj = json_decode($input, true);

$code = checkInput($obj['code']);

$appId = checkInput($obj['appId']);

$secret = $serverConfig->getCardWxSecret();

$AccessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appId . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";

// $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
//获取GET请求
function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//发送
$str = httpGet($AccessTokenUrl);
echo $str;
}
复制代码
posted @   叶乘风  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示