没有从cURL收到任何错误消息或内容

时间:2016-08-31 06:35:51

标签: php curl

使用下面的代码我没有收到任何错误或内容。

我有兴趣获取我的网站http://www.aptamitra.in的内容。

$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
//$timeout = 5;
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, "http://www.aptamitra.in" );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
//curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
//curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
//curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
var_dump($content);
echo "<pre>";
print_r($response);  // iam getting response here.
curl_close ( $ch );

我使用var_dump($content) print_r($response)使用的数组如下所示

bool(false)

Array
(
    [url] => http://www.aptamitra.in/
    [content_type] => text/html; charset=UTF-8
    [http_code] => 302
    [header_size] => 4956
    [request_size] => 4984
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 20
    [total_time] => 0.452
    [namelookup_time] => 0
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => -1
    [starttransfer_time] => 0.124
    [redirect_time] => 0.328
    [redirect_url] => 
    [primary_ip] => 127.0.0.1
    [certinfo] => Array
        (
        )

    [primary_port] => 1080
    [local_ip] => 127.0.0.1
    [local_port] => 52230
)

我不知道为什么没有内容。

3 个答案:

答案 0 :(得分:2)

根据curl_exec()文档:

  

成功时返回TRUE,失败时返回FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将在成功时返回结果,在失败时返回FALSE。

您对var_dump($content);的来电显示bool(false),表示curl_exec()失败。这就是为什么你没有得到任何回复内容。使用curl_errno()和/或curl_error()找出失败的原因。

我注意到的一件事是curl_getinfo()正在报告[redirect_count] => 20。这是重定向的很多。服务器很可能陷入无休止的重定向循环,curl_exec()在一段时间后决定失败。见CURLOPT_MAXREDIRS。检查报告的错误编号是否为CURLE_TOO_MANY_REDIRECTS

答案 1 :(得分:0)

希望这有效

 $options = array(
  CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
  CURLOPT_POST           =>false,        //set to GET
  CURLOPT_USERAGENT      => 'Mozilla/5.0 (Windows NT 6.1; rv:48.0) Gecko/20100101 Firefox/48.0', //set user agent
  CURLOPT_RETURNTRANSFER => true,     // return web page
  CURLOPT_HEADER         => false,    // don't return headers
  CURLOPT_FOLLOWLOCATION => true,     // follow redirects
  CURLOPT_ENCODING       => "",       // handle all encodings
  CURLOPT_AUTOREFERER    => true,     // set referer on redirect
  CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
  CURLOPT_TIMEOUT        => 120,      // timeout on response
  CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
);

$ch      = curl_init("http://www.aptamitra.in/");
curl_setopt_array( $ch, $options );    
echo curl_exec( $ch );    
curl_close( $ch );

答案 2 :(得分:0)

使用以下我能够打印页面内容确定。

function mmcurl( $url=NULL, $data=array(), $options=NULL ){

    /* Download cacert.pem and change path here to suit */
    $cacert='c:/wwwroot/cacert.pem';
    $cookiejar=tempnam( sys_get_temp_dir(), '_cookiejar_' );                    


    $curl=curl_init();
    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
    }
    curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
    curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
    curl_setopt( $curl, CURLOPT_HEADER, FALSE );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0' );
    curl_setopt( $curl, CURLOPT_ENCODING, '' );

    if( $options->cookie ){
        curl_setopt( $curl, CURLOPT_COOKIEFILE, $cookiejar );
        curl_setopt( $curl, CURLOPT_COOKIEJAR, $cookiejar );
        curl_setopt( $curl, CURLOPT_COOKIE, $cookiejar );
    }
    if( $options->post ){
        curl_setopt( $curl, CURLOPT_POST, true );
        curl_setopt( $curl, CURLOPT_POSTFIELDS, http_build_query( $data ) );
    }
    $res=(object)array(
        'response'  =>  curl_exec( $curl ),
        'info'      =>  curl_getinfo( $curl ),
        'errors'    =>  curl_error( $curl ),
        'cookie'    =>  $cookiejar
    );
    curl_close( $curl );
    return $res;
}



$url='http://www.aptamitra.in';
$data=array();
$options=new StdClass;
$options->post=false;
$options->cookie=true;


$res=mmcurl( $url, $data, $options );
echo '<pre>',print_r($res,true),'</pre>';