0

I'm trying to extract a small amount of data from another website but I am finding that it is timing out. I've put some coding clauses in place to try and force my code to obtain data from a similar data if trying to obtain it from the first takes too long.

$ctx = stream_context_create(array('http' => array('timeout' => 2)));

$geo = json_decode(file_get_contents($url, 0, $ctx), true);

In principle this should time out after 2 seconds and then I'll know its duff and go for the second URL. But it doesn't work, and tries to get it for 15 seconds. I've tried recplacing the $ctx with

ini_set('default_socket_timeout', 2);

But this doesn't work either. Can anyone please suggest something else I could try?

2 Answers2

0

In my case when the URL begins with https the timeout does not work. But when I removed the s from the end of the https it worked. If you can allow your script to use the non secure protocol your problem is solved.

Janos
  • 1
  • 2
-1

There is another way you can set the timeout..

It would be with stream_context_create().

$ctx = stream_context_create(array('http'=>
array(
    'timeout' => 1200, // 1 200 Seconds = 20 Minutes
)
));

echo file_get_contents('the link', false, $ctx);

Have a look here

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26