2015-11-01 242 views
1

我有這個功能捲曲的URL(在我的笨網站):捲曲返回空白API

private function _curl_download($Url){ 

    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $Url); 
    curl_setopt($ch, CURLOPT_REFERER, "http://local.mywebsite.com"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    if(curl_errno($ch)){ 
     echo 'Curl error: ' . curl_error($ch); 
    } 
    $output = curl_exec($ch); 
    print_r(curl_getinfo($ch)); 
    curl_close($ch); 

    return $output; 
} 

這是假設返回我JSON,但它實際上是空白...

$url = 'https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?locale=fr_FR&api_key=myprivateapikey'; 
$info_champion = $this->_curl_download($url); 
print_r($info_champion); 

這裏是什麼curl_getinfo($ch)回我:

Array 
(
    [url] => https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?locale=fr_FR&api_key=mysecretapikey 
    [content_type] => 
    [http_code] => 0 
    [header_size] => 0 
    [request_size] => 0 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 0.391 
    [namelookup_time] => 0 
    [connect_time] => 0.219 
    [pretransfer_time] => 0 
    [size_upload] => 0 
    [size_download] => 0 
    [speed_download] => 0 
    [speed_upload] => 0 
    [download_content_length] => -1 
    [upload_content_length] => -1 
    [starttransfer_time] => 0 
    [redirect_time] => 0 
    [redirect_url] => 
    [primary_ip] => 52.8.176.206 
    [certinfo] => Array 
     (
     ) 

    [primary_port] => 443 
    [local_ip] => 192.168.1.104 
    [local_port] => 57748 
) 

在瀏覽器中的鏈接工作,所以這不是問題。

我不明白什麼是錯的。謝謝你的幫助。

+0

當API成功時,URL是否被重定向到別的地方?在這種情況下,您需要使用curl來關注該URL。 – itoctopus

+0

不,網址沒有重定向到 – Leeroy521

+0

檢查您是否能夠從正在運行您的代碼的服務器連接到主機 –

回答

0

根據我的經驗,當處理https的連接時,您需要爲curl請求添加更多參數並獲得cacert.pem的副本。

private function _curl_download($url){ 
    /* edit path to suit environment */ 
    $cacert='c:/wwwroot/cacert.pem'; 

    /* download a copy from: http://curl.haxx.se/ca/cacert.pem */ 

    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    $ch = curl_init(); 
    if(parse_url($url,PHP_URL_SCHEME)=='https'){ 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 
     curl_setopt($curl, CURLOPT_CAINFO, realpath($cacert)); 
    } 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_REFERER, "http://local.mywebsite.com"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    if(curl_errno($ch)){ 
     echo 'Curl error: ' . curl_error($ch); 
    } 
    $output = curl_exec($ch); 
    print_r(curl_getinfo($ch)); 
    curl_close($ch); 

    return $output; 
} 
0

感謝@RamRaider我做了一個小的搜索和我發現,我要補充一點:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

,一切都正確。