cURL发送表单但远程服务器没有POST

时间:2012-02-10 09:29:37

标签: php html curl

我正在创建一个使用PHP cURL自动发送表单的插件。它的结果很好但是表单没有被服务器发布。那是为什么?

//create array of data to be posted
$post_data['full_name'] = $name;
$post_data['email'] = $email;
$post_data['subscription_type'] = 'E';
$post_data['id'] = $id;

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = curl_init($target_url);

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection).'-'.curl_error($curl_connection);

//close the connection
curl_close($curl_connection);

这是代码的结果。

Array ( 
[url] => http://www.remote-server.com/formcapture.php?v=28&w=29 
[content_type] => text/html 
[http_code] => 200 
[header_size] => 180 
[request_size] => 302 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 0.742828 
[namelookup_time] => 0.015174 
[connect_time] => 0.062791 
[pretransfer_time] => 0.0628 
[size_upload] => 75 
[size_download] => 6122 
[speed_download] => 8241 
[speed_upload] => 100 
[download_content_length] => 6122 
[upload_content_length] => 75 
[starttransfer_time] => 0.692776 
[redirect_time] => 0 
) 0- 

这是我要发送的表单。

<form class='subscription_form' id='subscription_form' method='POST' action='http://www.remote-server.com/formcapture.php?v=28&w=29'>
<div align='center'><center>
<p>Full name<br><input type='text' name='full_name' size='20'></p>
</center></div>
<div align='center'><center>
<p>E-mail address<br><input type='text' name='email' size='20'></p>
</center></div>
<input type='hidden' name='subscription_type' value='E'><div align='center'><center>
<p><input type='submit' value='Go »'></p>
</center></div>
<input type='hidden' name='id' value='360'>
</form>

1 个答案:

答案 0 :(得分:0)

您是否尝试过设置cURL以返回响应标头?

curl_setopt($curl_connection, CURLOPT_HEADER, 1);

这应该会带回您可以回显的标题,并用于查看其他服务器正在返回的内容。

另外:

echo curl_getinfo($curl_connection, CURLINFO_HTTP_CODE);

应该带回HTTP响应代码,例如200 404 500等