今天我遇到了一种情况。

我正在使用file_get_contents从用户文件中获取 token 。

$data=file_get_contents("http://example.com/aaa.php?user=tester&akey=abcdef1234");
$dec=json_decode($data,true);
$tokenid=$dec['message']['result']['tokenid'];

使用 token ,我将调用另一个文件以获取详细信息;
$data=file_get_contents("http://example.com/bbb.php?user=tester&token=".$tokenid);

问题是有时我没有得到tokenid,刷新页面后我得到了它。

在aaa.php中没有问题,它的工作正常。

我怀疑php在进入第二个file_get_contents之前是否不等待 token 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);

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

关于php - file_get_contents同步或异步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22105075/

10-11 22:59
查看更多