开发者

PHP page hangs on this block of code

开发者 https://www.devze.com 2023-03-14 02:08 出处:网络
Recently, with no changes to my code, my PHP page started to hang at a certain area. It generates all of the HTML on the page right before this line:

Recently, with no changes to my code, my PHP page started to hang at a certain area. It generates all of the HTML on the page right before this line:

$tickerJSON = file_get_contents("http://mtgox.com/code/data/ticker.php");

I commented out everything else and this is the cause of the error.

I 开发者_运维百科know that that JSON url is valid and the array names are correct. I'm not sure where the problem is in this case. Any help?

Note: It doesn't display a partial or white page, it'll keep loading forever with no display output.


The problem is that the remote server appears to be purposely stall requests that don't send a user agent string. By default, PHP's user-agent string is blank.

Try adding this line directly above your call:

ini_set('user_agent', 'PHP/' . PHP_VERSION);

I've tested the above using this script and it worked great for me:

<?php

ini_set('user_agent', 'PHP/' . PHP_VERSION);

$tickerJSON = file_get_contents("http://mtgox.com/code/data/ticker.php");

echo $tickerJSON;


Update:

$tickerJSON = shell_exec('wget --no-check-certificate -q -O - https://mtgox.com/code/data/ticker.php');

The remote connection you do takes a very long time. You can go around with that providing a timeout value. If it takes too long, the function won't return any data but it wont hinder the script as well from continuing to run.

Next to that you need to set the user-agent:

// Create a stream
$opts = array(
  'http'=>array(
    'timeout'=> 3, // 3 second timeout
    'user_agent'=> 'hashcash',
    'header'=>"Accept-language: en\r\n"
  )
);
$context = stream_context_create($opts);
$url = "https://mtgox.com/code/data/ticker.php";
$tickerJSON = file_get_contents($url, FALSE, $context);
0

精彩评论

暂无评论...
验证码 换一张
取 消