douzui0143 2013-02-13 16:32
浏览 250
已采纳

PHP CURL使用代理进行POST

I'm trying to create a script that posts to an URL, using a list of proxies. The script seems to work fine without setting a proxy (CURLOPT_PROXY to null), but with a proxy set it does not seem to work. Does anybody know what I'm doing wrong here? The error I'm getting when posting with a proxy is:

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.

And my code is:

$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;
  • 写回答

1条回答 默认 最新

  • doude4201 2013-02-13 16:35
    关注

    You are missing the PROXYPORT option... You can't use the proxy:port notation

    curl_setopt($ch, CURLOPT_PROXY,             "YOUR PROXY HOST");         
    curl_setopt($ch, CURLOPT_PROXYPORT,         "YOUR PROXY PORT");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名