PHP cURL 导致 403 Forbidden

标签 php curl http-status-code-403

我尝试 cURL 远程站点,结果出现 403 Forbidden 错误。我可以从同一服务器通过终端运行以下两个命令。第一个失败,而第二个有效。如何让我的 PHP 代码与第二个终端命令相匹配?

此终端命令不会返回任何结果。

curl http://www.barneys.com

此终端命令会产生正确的结果(页面的 HTML)

curl -L http://www.barneys.com

我的 PHP 代码:

$ch = curl_init('http://www.barneys.com');
$http_headers = array(
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:40.0)' . 'Gecko/20100101 Firefox/40.0',
'Accept: */*',
'X-Requested-With: XMLHttpRequest',
'Referer: http://www.barneys.com', # IMPORTANT
'Accept-Language: pt-BR,en-US;q=0.7,en;q=0.3',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.barneys.com');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$response = curl_exec($ch);
$redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
curl_close($ch);

echo $response;

编辑:这是通过 PHP 进行的 cURL 请求的日志:

* About to connect() to www.barneys.com port 80 (#0)
*   Trying 23.204.27.110... * connected
* Connected to www.barneys.com (23.204.27.110) port 80 (#0)
> GET / HTTP/1.1
Host: www.barneys.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:40.0)Gecko/20100101 Firefox/40.0
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://www.barneys.com
Accept-Language: pt-BR,en-US;q=0.7,en;q=0.3
Connection: keep-alive

< HTTP/1.1 403 Forbidden
< Server: AkamaiGHost
< Mime-Version: 1.0
< Content-Type: text/html
< Content-Length: 265
< Expires: Mon, 11 Apr 2016 23:22:16 GMT
< Date: Mon, 11 Apr 2016 23:22:16 GMT
< Connection: close
< 
* Closing connection #0

最佳答案

您的 php cURL 请求中不包含请求 header 。要解决此问题,请在设置 CURLOPT_HTTPHEADER 选项之前添加以下行:

curl_setopt($ch, CURLOPT_HEADER, true);

来自PHP curl docs :

CURLOPT_HEADER TRUE to include the header in the output.

此外,如果您在 URL 中添加尾部斜杠,则 URL 不需要由 cURL 重新构建。您的很多代码似乎都是不必要的。这是一个精简的工作示例:

<?php
$ch = curl_init('http://www.barneys.com/');
$http_headers = array(
    'User-Agent: Junk', // Any User-Agent will do here
);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

关于PHP cURL 导致 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36555908/

相关文章:

php - 使用滚动面板修复表头

php - 省略 PHP 三元和空值合并运算符中的 'else'

php - 将命令 CURL 转换为 PHP CURL?云火焰 API

google-maps-api-3 - 经过几个月的 100% 稳定性,今天我在谷歌地图服务上收到 403 错误

apache - Windows 7 上的 WAMP 403 禁止消息

php - 为什么 $_REQUEST 是空的

php - AJAX FormData Post - 503 服务不可用

node.js - 使用 Node 请求 OAuth 凭据

php - file_get_contents 返回 403 禁止

django - 如何让 Django 在 AWS S3 上将某些文件设为公开,将媒体文件设为私有(private)(无 403 错误)?