2014-01-25 60 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); 
     } 


?>