file_get_contents无法使用php文件

时间:2014-12-23 15:32:32

标签: php json api

代码:

$btc38="http://api.btc38.com/v1/depth.php?c=ltc&mk_type=btc";
$btc38_r=file_get_contents($btc38);
$btc38_a=json_decode($btc38_r,true);

我使用过其他网站的API并且他们工作了,唯一没有工作的就是上面的那个。 所有有效的网站都不使用像上面那样的PHP文件(depth.php),所以这可能就是问题所在。

所以我的问题是,有没有其他方法可以将该链接解析为多维数组?

编辑:var_dump仅用于调试,我的目的是将链接解析为数组。

1 个答案:

答案 0 :(得分:4)

请勿使用打印输出的var_dump()。并设置一些用户代理。没有这个,我就会被禁止回来:

$url = "http://api.btc38.com/v1/depth.php?c=ltc&mk_type=btc";
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Content-type: application/json\r\n" .  // check function.stream-context-create on php.net
              "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
  )
);
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
$btc38_a = json_decode($file, true);
var_dump($btc38_a);
相关问题