【问题标题】:PHP CURL use proxy to POSTPHP CURL 使用代理 POST
【发布时间】:2013-02-13 16:32:36
【问题描述】:

我正在尝试使用代理列表创建一个发布到 URL 的脚本。该脚本似乎可以在不设置代理(CURLOPT_PROXY 为 null)的情况下正常工作,但如果设置了代理,它似乎无法正常工作。有人知道我在这里做错了什么吗?我在使用代理发帖时遇到的错误是:

ERROR

The requested URL could not be retrieved

Invalid Request error was encountered while trying to process the request:

POST /###/ HTTP/1.1
Host: ###
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 368
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------9b24e849cac0
Some possible problems are:

Missing or unknown request method.

Missing URL.

Missing HTTP Identifier (HTTP/1.0).

Request is too large.

Content-Length missing for POST or PUT requests.

Illegal character in hostname; underscores are not allowed.

我的代码是:

$url = 'urltopost';
$proxy = 'proxy:port';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'postname1' => 'value1',
    'postname2' => 'value2'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

【问题讨论】:

    标签: php post curl proxy


    【解决方案1】:

    您缺少 PROXYPORT 选项...您不能使用 proxy:port 表示法

    curl_setopt($ch, CURLOPT_PROXY,             "YOUR PROXY HOST");         
    curl_setopt($ch, CURLOPT_PROXYPORT,         "YOUR PROXY PORT");
    

    【讨论】:

    • 现在似乎可以工作了。当我没有添加任何 POST 变量时,proxy:port 表示法的工作方式非常奇怪......但感谢您的快速回答!
    • 嘿,虽然我不同意这个解决方案,但你能解释一下为什么它不能以其他方式工作吗?我找不到任何地方...所有示例(以及 curl_setopt() 的 PHP 手册)将端口和主机放在一起...谢谢!
    • 如果我没记错的话,全局 env vars 也足够了吗?
    猜你喜欢
    • 2015-02-05
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多