php curl无法正常工作,返回空页,我怎样才能模拟真正的浏览器

时间:2015-03-14 15:22:28

标签: php curl php-curl

我想从网站上获取一些细节,我的代码在wampserver中正常工作但是当我在真实服务器上上传文件时我没有得到任何结果,这里显示我的空页是我的代码。

    <?php 
error_reporting(E_ALL);

$htm = my_curl($url);
echo $htm;
$dat = file_get_contents('cookies.txt');

echo "<pre>";
echo PHP_EOL . "<b>$url</b>";
echo PHP_EOL . "<i>$dat</i>";
echo PHP_EOL;


function my_curl( $url, $timeout=5, $error_report=TRUE)
{
    $curl = curl_init();


    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: timeout=5, max=100";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = ""; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookies.txt' );
    curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookies.txt' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );


    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {   echo 'HELO';
        if ($error_report)
        {echo 'HELOs';
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

我已经检查了我的php配置,一切似乎都没问题。我在Stack Overflow中的任何其他相关帖子中找不到任何答案。

错误:

  

CURL FAIL:http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07 TIMEOUT = 5,CURL_ERRNO = 7array(22){[“url”] =&gt; string(86)“http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07”[“content_type”] =&gt; NULL [“http_code”] =&gt; int(0)[“header_size”] =&gt; int(0)[“request_size”] =&gt; int(0)[“filetime”] =&gt; int(-1)[“ssl_verify_result”] =&gt; int(0)[“redirect_count”] =&gt; int(0)[“total_time”] =&gt; float(0)[“namelookup_time”] =&gt; float(0.001099)[“connect_time”] =&gt; float(0)[“pretransfer_time”] =&gt; float(0)[“size_upload”] =&gt; float(0)[“size_download”] =&gt; float(0)[“speed_download”] =&gt; float(0)[“speed_upload”] =&gt; float(0)[“download_content_length”] =&gt; float(-1)[“upload_content_length”] =&gt; float(-1)[“starttransfer_time”] =&gt; float(0)[“redirect_time”] =&gt; float(0)[“certinfo”] =&gt; array(0){} [“redirect_url”] =&gt; string(0)“”}

2 个答案:

答案 0 :(得分:0)

尝试单独设置端口。

curl_setopt($ch, CURLOPT_PORT, 7001);

答案 1 :(得分:0)

我认为您的网址无效 - 目前您的网址是:

http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspxrollno=123&sem=456

虽然我认为它应该是:

http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=123&sem=456

添加?在aspx和rollno之间再试一次