Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 此站点是否阻止/忽略我的HTTP请求?_Php_Http_Curl - Fatal编程技术网

Php 此站点是否阻止/忽略我的HTTP请求?

Php 此站点是否阻止/忽略我的HTTP请求?,php,http,curl,Php,Http,Curl,我只有在使用cURL和代理时才能从站点获取。没有代理的cURL和文件\u get\u contents()不返回任何内容(cURL HTTP代码“0”和cURL\u error() 来自服务器的空回复)。我可以在没有代理的情况下获取其他站点 除了被阻止之外,还有其他可能的解释吗?为什么我只能通过代理访问此站点?您是否在cURL中设置了用户代理?有时,如果您的用户代理未设置或您的HTTP请求看起来可疑,网站会阻止您 要在PHP中设置用户代理,请执行以下操作: curl_setopt($curl,

我只有在使用cURL和代理时才能从站点获取。没有代理的cURL和
文件\u get\u contents()
不返回任何内容(cURL HTTP代码“0”和cURL\u error()
来自服务器的空回复
)。我可以在没有代理的情况下获取其他站点


除了被阻止之外,还有其他可能的解释吗?为什么我只能通过代理访问此站点?

您是否在cURL中设置了用户代理?有时,如果您的用户代理未设置或您的HTTP请求看起来可疑,网站会阻止您

要在PHP中设置用户代理,请执行以下操作:

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

这是来自你的工作场所还是别的什么?许多公司在共享PHP安装上禁用
file\u get\u contents()
,因为这相当危险

该站点可能有用户代理检测。你可以在curl调用中假装,但我不相信这在
file\u get\u contents()
中是可能的。网站使用的另一种方法是仅在设置cookie后显示内容,这样网站爬虫将永远看不到数据

试试这个:

function curl_scrape($url,$data,$proxy,$proxystatus)
{
    $fp = fopen("cookie.txt", "w");
    fclose($fp);
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if ($proxystatus == 'on')
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    ob_start(); // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean(); // stop preventing output
    curl_close ($ch);
    unset($ch);
}

我猜我真的被封锁了。现在使用代理,它工作正常。

echo curl\u error($curl)的输出是什么?更新为curl_error()outputAlready测试了cookie的想法(它不关心cookie),并根据其他答案假装用户代理。。没有骰子