简体   繁体   English

卷曲返回空白api

[英]Curl returning blank api

I've this function to curl an url (in my codeigniter website): 我具有此功能来卷曲url(在我的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;
}

This is suppose to return me a json but it's actually blank... 这是想给我返回一个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);

Here is what the curl_getinfo($ch) return me : 这是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
)

The link work in my browser so this is not the problem. 该链接在我的浏览器中有效,所以这不是问题。

I don't understand what can be wrong. 我不明白有什么问题。 Thanks for your help. 谢谢你的帮助。

In my experience when dealing with connections over https you need to add more parameters to the curl request and have a copy of cacert.pem available. 根据我在处理通过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;
}

Thanks to @RamRaider I've made a little search and I've found that I have to add this : 感谢@RamRaider我进行了一些搜索,发现我必须添加以下内容:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );

and everything went right. 一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM