2017-06-17 102 views
0

我有一段非常簡單的代碼,它應該返回一個JSON對象,但不返回任何值。php file_get_contents不返回值

當我直接訪問url https://api.kraken.com/0/public/Time時,可以看到所需的JSON輸出。

但是調用一個函數(在URL https://youreka-virtualtours.be/ethereum/functions.php?call=get_kraken)的時候,我沒有得到任何輸出

<?php 
    if (isset($_GET['call'])) { 
     $error = ''; 
     switch($_GET['call']) { 
      case 'get_kraken': 
        $url = 'https://api.kraken.com/0/public/Time'; 
        $json = file_get_contents($url); 

        http_response_code(200); 
       header('Content-Type: application/json'); 
       echo $json; 
       break; 
     } 
    } 
?> 

編輯:問:自從我的file_get_contents的輸出什麼,我做錯了從什麼不同url https://api.kraken.com/0/public/Time顯示

+0

而你的問題是? – hakre

+0

使用curl而不是file_get_contents來檢查響應http代碼,它可以幫助您調試問題出在哪裏。 –

+0

**我得到** SyntaxError:JSON.parse:JSON數據第1行第3列的數據意外結束 – RiggsFolly

回答

0

代碼工作正常,我在本地測試它。這意味着兩件事情之一正在發生。 1:您的服務器無法進行file_get_contents調用。 2:你的網址不正確,你真的應該回應一些事情以防萬一。試試這個測試:

<?php 
    if(!isset($_GET['call']) || "get_kraken" !== @$_GET['call']) { 
     echo "could not find a valid URL"; 
    }elseif (isset($_GET['call'])) { 
     $error = ''; 
     switch($_GET['call']) { 
      case 'get_kraken': 
        $url = 'https://api.kraken.com/0/public/Time'; 
        $json = file_get_contents($url); 

        http_response_code(200); 
       header('Content-Type: application/json'); 
       echo $json; 
       break; 
     } 
    } 
?> 
+0

當說它工作正常,你看到直接進入https://api.kraken.com/0/public/Time時看到相同的輸出? –