2014-01-25 45 views
2

我想获得一个代码脚本的工作,但我得到

未定义的变量:http_response_header

错误。我搜索了一下,发现$ http_response_header是全局的,并且内置到php中,为什么我会得到这个错误。我正在使用安装PHP 5.4的wamp。这里是我的代码:

$url="https://data.mtgox.com/api/2/BTCCAD/money/ticker"; 
$json = @file_get_contents($url); 

//check for errors 
if (strpos($http_response_header[0], "200")) { 
    // On success, decode JSON 
    $data = json_decode($json); 

感谢 艾哈迈尔

+1

你在哪里读了'$ http_response_header'是全球性的? – Fractaliste

+0

该文档明确指出'$ http_response_header'不是全局的。 http://www.php.net/manual/en/reserved.variables.httpresponseheader.php我建议反正使用cURL。 – Brad

+0

@Brad我正试图让这个股票工作。但是它使用http标题响应http://skybin.net/bitcoin-ticker-on-your-website/ –

回答

1
$content = file_get_contents("https://data.mtgox.com/api/2/BTCCAD/money/ticker"); 

if(!empty($content)) 
{ 
    $data = json_decode($content); 

    if (json_last_error() !== JSON_ERROR_NONE) 
    { 
     die("incorrect data"); 
    } 
} 
1

嗨,如果我理解你想做的事。我在php 5.5中测试了下面的代码:

试试吧。

<?php 
    function get_contents() { 

     $url="https://data.mtgox.com/api/2/BTCCAD/money/ticker"; 
     $json = file_get_contents($url); 

     if(strpos($http_response_header[0], "200")){ 
     $data = json_decode($json); 
      //you return $data 
      //return $data; 

     var_dump($data); 
     } 

    } 

    get_contents(); // call the function 

,如果你不喜欢使用该功能:

$url="https://data.mtgox.com/api/2/BTCCAD/money/ticker"; 
     $json = file_get_contents($url); 
     if(strpos($http_response_header[0], "200")){ 
     $data = json_decode($json); 
     var_dump($data); 
     } 


?>