2013-10-31 48 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