file_get_contents同步或异步

用户名

今天我遇到了一种情况。

我正在使用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中没有问题,它的工作正常。

我怀疑php是否file_get_contents在第二秒之前不等待令牌响应file_get_contents(asynchronous);

我也尝试过curl,但是有时我没有得到tokenid。我还没有遇到过这类问题。

榕树

绝对不是同步与异步的问题。但是调试几乎是不可能的。尝试这样的事情。这些die声明很丑陋,但说明了您可能要合并的验证...

$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);

一个猜测可能是您的令牌有时包含需要转义的“特殊”字符。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章