今天我遇到了一种情况。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);
问题是有时我没有得到令牌ID,刷新页面后,我得到它。
aaa.php没有问题,它的工作正常。
我怀疑是否PHP不等待令牌的file_get_contents
的响应才去第二file_get_contents(asynchronous);
我与卷曲尝试过,但有时我没有得到tokenid。我没有遇到这类问题。
'file_get_contents'肯定是同步的。一般来说PHP是同步的。 – Barmar
“没有得到”如何? '$ data'出现空白?包含除json之外的其他内容,导致'$ dec'为空? f_g_c不是异步的,并且会在接收数据或底层网络内容超时之前阻塞。 –
很多时候我收到json与tokenid,但有时我收到null。 – user1602452