file_get_contents synchronous or asynchronous

佐手、 提交于 2019-12-05 14:25:18

Definitely not a question of synchronous vs. asynchronous. But as is debugging is pretty impossible. Try something like this. The die statements are ugly but illustrates the validation you might want to incorporate...

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

A guess might be your token sometimes contains 'special' characters that need to be escaped.

Mikhail Gerasimov

file_get_contents is synchronous. You can get FALSE sometimes because of different reasons like network fail, DNS fail etc.

Use curl instead: it's faster and more customizable. You can wait for good response recursive if you need 100% success.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!