【发布时间】:2012-04-29 02:05:51
【问题描述】:
我在使用 PHP 中的 cURL 脚本发送 POST 请求时遇到问题。
我正在尝试制作一个代理,主要是为了我自己的个人使用,它将通过服务器获取网页并在本地显示给我。
网址如下所示:http://fetch.example.com/http://theurl.com/
当我在该页面上的表单中发帖时,它将转到表单的 ACTION(前面带有获取 URL)。我正在尝试使用下面的代码让它处理这个 POST 请求,但是我发布的任何内容总是会带来 400 Bad Request 错误。
$chpg = curl_init();
curl_setopt($chpg, CURLOPT_URL, $_URL);
curl_setopt($chpg, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpg, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chpg, CURLOPT_COOKIESESSION, true);
curl_setopt($chpg, CURLOPT_COOKIEJAR, "cookies/$_COOKIE_FILE.$_DOMAIN.txt");
curl_setopt($chpg, CURLOPT_COOKIEFILE, "cookies/$_COOKIE_FILE.$_DOMAIN.txt");
if($_POST) {
$fields = array();
foreach($_POST as $col => $val) {
$fields[$col] = urlencode($val);
}
print_r($fields);
curl_setopt($chpg, CURLOPT_POST, count($fields));
curl_setopt($chpg, CURLOPT_POSTDATA, $fields);
}
【问题讨论】: