卷毛返回空白api

时间:2015-11-01 13:47:11

标签: php codeigniter curl local

我有这个功能来卷曲一个网址(在我的codeigniter网站上):

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
)

链接在我的浏览器中工作,所以这不是问题。

我不明白什么是错的。谢谢你的帮助。

2 个答案:

答案 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;
}

答案 1 :(得分:0)

感谢@RamRaider我做了一些搜索,我发现我必须添加这个:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );

一切都很顺利。