使用PHP自动提交表单

时间:2014-10-08 12:36:37

标签: php forms curl


我想提交一个带有特殊值的php的表单,但这里的代码不是代码:

<?php
//create array of data to be posted
$post_data['username'] = 'something';
$post_data['password'] = 'something!';
//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('http://www.parsdata.com/');
//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.parsdata.com/ [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 328 [request_size] => 245 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 3.876425 [namelookup_time] => 0.031218 [connect_time] => 0.238176 [pretransfer_time] => 0.23823 [size_upload] => 56 [size_download] => 72015 [speed_download] => 18577 [speed_upload] => 14 [download_content_length] => 72015 [upload_content_length] => 56 [starttransfer_time] => 1.703701 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 217.66.216.91 [primary_port] => 80 [local_ip] => 23.236.155.242 [local_port] => 43417 [redirect_url] => ) 0-

没有任何错误,但它不起作用 我真的不知道该怎么办。
如果你需要一个真实的用户并通过我可以给你!
提前谢谢。

3 个答案:

答案 0 :(得分:1)

您应该使用正确的URL进行登录。我访问了该网站,并找到了此登录网址 - http://members.parsdata.com/default.aspx?dll=user&ctl=autosignin

答案 1 :(得分:1)

实际上,它可能有效。

如果您想登录网站,您只需要解析响应中的Set-Cookie标题,并将Cookie标题传递给您要执行的每个请求之后。

您可以使用Cookie jar来执行此操作,此处有更多信息:PHP Curl And Cookies

答案 2 :(得分:0)

如果您想登录网站,您需要做的就是解析响应中的Set-Cookie标头,并将Cookie标头传递给您在dat之后执行的每个请求。