功能:
function post_with_curl($target,$ref, $name ,$viewStateValue )
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target) ; // Target site
curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/', getcwd().'/'."cook.txt" )); //CHANGE THIS
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME);
curl_setopt ($ch, CURLOPT_POST, 1);
$postfields = urlencode('__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=.'. $viewStateValue. '&__VIEWSTATEENCRYPTED=&ctl00$ContentPlaceHolder1$NameSearch1$CompanyNameTextBox1='.$name.'&ctl00$ContentPlaceHolder1$SearchButton=Search Now' ) ;
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
$curled_page = curl_exec($ch);
var_dump(curl_getinfo($ch , CURLINFO_HEADER_OUT)) ;
curl_close($ch);
return $curled_page ;
}
var_dump的结果(curl_getinfo($ ch,CURLINFO_HEADER_OUT)):
string 'POST /V2/COUNTY/Default.aspx HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Host: 198.173.15.31
Accept: */*
Referer: http://198.173.15.31/V2/COUNTY/
Cookie: ASP.NET_SessionId=pqfpur45akgy3l45ujq3fail
Content-Length: 1603
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
' (length=339)
在我看来,标题(由curl发送?)不包含任何POST数据。为什么?
答案 0 :(得分:2)
这是因为post数据不是HTTP消息头部的一部分,而是HTTP消息体的一部分。如果你得到标题,你就不会得到身体。如果要检索正文,则必须检索邮件正文。
顺便说一句,您不需要检索它,因为您已经将它自己创建到$postfields
变量中。这只是发布的数据。
或者我误读了你实际要求的东西?