2013-10-31 53 views
2

嗨,我試圖使用從JSON文件從URL數據捲曲和PHP使用此代碼來獲得:通過捲曲從URL獲取JSON以https返回Null

<?php 
    // Initiate curl 
    $url = "https://panel.virtualcenter360.es/home/api/getdatasample?dateIni=1376524800&dateEnd=1381795200"; 
    $ch = curl_init($url); 
    // Set the url 
    curl_setopt($ch, CURLOPT_URL,$url); 
    // Execute 
    $result=curl_exec($ch); 

    $res = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($result)); 

    var_dump(json_decode($res, true)); 

     switch (json_last_error()) { 
    case JSON_ERROR_NONE: 
     echo ' - No errors'; 
    break; 
    case JSON_ERROR_DEPTH: 
     echo ' - Maximum stack depth exceeded'; 
    break; 
    case JSON_ERROR_STATE_MISMATCH: 
     echo ' - Underflow or the modes mismatch'; 
    break; 
    case JSON_ERROR_CTRL_CHAR: 
     echo ' - Unexpected control character found'; 
    break; 
    case JSON_ERROR_SYNTAX: 
     echo ' - Syntax error, malformed JSON'; 
    break; 
    case JSON_ERROR_UTF8: 
     echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; 
    break; 
    default: 
     echo ' - Unknown error'; 
    break; 
} 
    curl_close($ch); 
?> 

,我發現了沒有錯誤消息,但爲NULL結果。你認爲這可能是什麼問題?

也許我需要從Curl配置一些參數?

謝謝!

回答

1

如果你想與捲曲工作,你必須這樣做:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

CURLOPT_SSL_VERIFYPEER
FALSE從驗證對方的證書停止捲曲。可以使用CURLOPT_CAINFO選項指定要驗證的替代證書,也可以使用CURLOPT_CAPATH選項指定證書目錄。

http://php.net/manual/en/function.curl-setopt.php

1

如果您只需要獲取一些json信息,我建議您使用file_get_contents()而不是cURL函數。

+0

我還試圖與file_get_contents()函數,如:$結果=的file_get_contents('https://panel.virtualcenter360.es/home/API/getdatasample dateIni = 1376524800&dateEnd = 1381795200' )?; var_dump(json_decode($ result,true));但我得到錯誤。 – victorgb6

+0

'$ result = file_get_contents('https://panel.virtualcenter360.es/home/api/getdatasample?dateIni=1376524800&dateEnd=1381795200'); var_dump(json_decode($ result,true));'對我來說工作正常。我建議你使用[高級REST客戶端](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo)對Chrome或其他測試工具進行試用。 – JayEffKay

+0

嗨,我已經嘗試過在線服務器中的file_gets_contents,它的工作原理。我認爲它是從本地主機服務器嘗試的問題。謝謝! – victorgb6

0

在笨應用/庫文件夾

header('Content-Length: ' . strlen($output)); //Comment line no. 267 of REST_Controller.php