Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents synchronous or asynchronous

Today I came across one situation.

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

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

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

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

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

like image 658
user1602452 Avatar asked Feb 28 '14 20:02

user1602452


People also ask

How does file_get_contents work?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Which is faster cURL or file_get_contents?

This is old topic but on my last test on one my API, cURL is faster and more stable. Sometimes file_get_contents on larger request need over 5 seconds when cURL need only from 1.4 to 1.9 seconds what is double faster.

What does file_get_contents return?

This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to length bytes. On failure, file_get_contents() will return false . file_get_contents() is the preferred way to read the contents of a file into a string.

Does file_get_contents cache?

file_get_contents() does not have caching mechanism. However, you can use write your own caching mechanism. UPDATE the previous if case is incorrect, now rectified by comparing to current time.


2 Answers

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.

like image 157
Mikhail Gerasimov Avatar answered Sep 25 '22 16:09

Mikhail Gerasimov


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.

like image 43
ficuscr Avatar answered Sep 21 '22 16:09

ficuscr