的file_get_contents同步或异步 [英] file_get_contents synchronous or asynchronous

查看:1966
本文介绍了的file_get_contents同步或异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我遇到一种情况。

Today I came across one situation.

我使用的file_get_contents 得到了用户的文件标记。

I am using file_get_contents to get token from a file for a user.

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

使用令牌,我会调用另外一个文件,以获得详细信息;

Using the token i will call another file to get details;

$data=file_get_contents("http://example.com/bbb.php?user=tester&token=".$tokenid);

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

the problem is sometimes i am not getting tokenid, after refreshing the page i get it.

有在aaa.php其工作正常没有问题的。

There is no problem in aaa.php its working fine.

我怀疑是否PHP是之前没有去第二个的file_get_contents(异步)等待令牌中的的file_get_contents 的响应;

I doubt whether php is not waiting for the response of the file_get_contents of token before going to the second file_get_contents(asynchronous);

我与卷曲尝试过,但有时我没有得到tokenid。我还没有遇到这类问题。

I have tried with curl too but sometimes I am not getting tokenid. I haven't faced these kind of issues.

推荐答案

绝对不同步和异步的问题。但作为正在调试是pretty不可能的。尝试这样的事情。在死亡语句是丑陋的,但是说明你可能要纳入验证...

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.

这篇关于的file_get_contents同步或异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆