【问题标题】:php curl is not working ,returning empty page , how can i emulate real browserphp curl不起作用,返回空页面,我如何模拟真实的浏览器
【发布时间】:2020-05-20 04:06:13
【问题描述】:

我想从网站获取一些详细信息,我的代码在 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 失败:http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07 TIMEOUT=5, CURL_ERRNO=7array(22) { ["url"]=> string(86) "http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07" ["content_type"]=> NULL ["http_code"]= > int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.001099) ["connect_time"]=> float(0) ["pretransfer_time"]= > float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) [ "download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"] => 数组(0) { } ["redirect_url"]=> 字符串(0) "" }

【问题讨论】:

  • 你试过file_get_contents() 吗?它也将 URL 作为参数。
  • 是的,这就是我一开始就尝试过的,但它也不起作用
  • 要么该站点的渲染时间超过 5 秒,要么您访问了错误的 IP/端口,或者您在此脚本和服务器之间没有明确的路径。需要开防火墙吗?
  • @halfer 我对 curl 概念有点陌生,我正在尝试从大学网站获取我的标记表。我的代码在 wampserver 上运行良好。你能解释一下为什么我的代码不能在真实服务器上运行吗?服务器。我可以让它工作吗
  • 您的真实服务器似乎无法访问此 IP 地址。也许有防火墙挡住了,也许根本禁止它进行任何外部连接。尝试在您选择的任何公共网站上进行简单提取,看看是否可行。

标签: php curl php-curl


【解决方案1】:

我认为您的网址无效 - 目前您的网址类似于:

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 之间再试一次

【讨论】:

    【解决方案2】:

    尝试单独设置端口。

    curl_setopt($ch, CURLOPT_PORT, 7001);

    【讨论】:

    • 不走运.. 仍然没有结果
    • 他的意思是CURLOPT_PORT 不是CURLOPT
    • @smottt 友友可以看看吗
    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2014-09-16
    • 2013-03-06
    • 2015-05-15
    相关资源
    最近更新 更多