php – file_get_contents同步或异步
发布时间:2020-12-13 17:26:45 所属栏目:PHP教程 来源:网络整理
导读:今天我遇到了一个情况. 我正在使用file_get_contents从用户的文件中获取令牌. $data=file_get_contents("http://example.com/aaa.php?user=testerakey=abcdef1234");$dec=json_decode($data,true);$tokenid=$dec['message']['result']['tokenid']; 使用令牌
今天我遇到了一个情况.
我正在使用file_get_contents从用户的文件中获取令牌. $data=file_get_contents("http://example.com/aaa.php?user=tester&akey=abcdef1234"); $dec=json_decode($data,true); $tokenid=$dec['message']['result']['tokenid']; 使用令牌我将调用另一个文件来获取详细信息; $data=file_get_contents("http://example.com/bbb.php?user=tester&token=".$tokenid); 问题是有时我没有得到tokenid,刷新页面后我得到它. aaa.php没有问题,它的工作正常. 我怀疑在转到第二个file_get_contents(异步)之前php是否还没有等待令牌的file_get_contents的响应; 我也尝试过curl但有时候我没有得到tokenid.我没有遇到过这类问题. 解决方法
绝对不是同步与异步的问题.但是调试是非常不可能的.尝试这样的事情.死亡陈述很难看,但说明了你可能希望合并的验证……
$data = file_get_contents("http://example.com/aaa.php?user=tester&akey=abcdef1234"); if (empty($data)) die('Failed to fetch data'); $dec = json_decode($data,true); if (is_null($dec) || $dec === false) die('Failed to decode data'); $tokenid = isset($dec['message']['result']['tokenid']) ? $dec['message']['result']['tokenid'] : null; if (is_null($tokenid) die('Token ID is not set'); //... $data=file_get_contents("http://example.com/bbb.php?user=tester&token=".$tokenid); 猜测可能是您的令牌有时包含需要转义的“特殊”字符. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |