我正在尝试在此页http://212.202.102.92/abcrestore/search.aspx
上使用curl搜索产品,但遇到同样的问题。
首先,我正在尝试保存cookie并查找帖子变量... viewstate,eventarget,然后我将它们与curl一起使用
问题是我没有结果或更好地说我有所有结果。 我认为问题在于变量和cookie,但我不知道出了什么问题(可能所有代码都错了?)
搜索示例:CAS编号:1631-26-1,4814-74-8。
// headers
$header[] = "Host: 212.202.102.92";
$header[] = "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
$header[] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Accept-Language: en";
$header[] = "Accept-Encoding: gzip,deflate";
$header[] = "Accept-Charset: windows-1250,utf-8;q=0.7,*;q=0.7";
$header[] = "Keep-Alive: 115";
$header[] = "Connection: keep-alive";
$fp=fopen('abcr-temp.html','a');
fclose($fp);
//open connection
$ch = curl_init();
$fp=fopen('abcr-temp.html','w');
curl_setopt($ch, CURLOPT_URL, 'http://212.202.102.92/abcrestore/product_information.aspx?product_id=1890&second_id=1890&nav=search');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//set cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/abcrcookie.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
fclose($fp);
$html = file_get_contents('abcr-temp.html');
// find post variables
preg_match('~<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="(.*?)" />~',$html,$matches_lastfocus);
preg_match('~<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="(.*?)" />~',$html,$matches_eventargument);
preg_match('~<input type="hidden" name="__PREVIOUSPAGE" id="__PREVIOUSPAGE" value="(.*?)" />~',$html,$matches_previouspage);
//preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~',$html,$matches_eventvalidation);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~',$html,$matches_viewstate);
preg_match('~<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="(.*?)" />~',$html,$matches_viewstateencrypted);
$postArray['__LASTFOCUS'] = $matches_lastfocus[1];
$postArray['__EVENTTARGET'] = 'ctl00$btnSuchen';
$postArray['__EVENTARGUMENT'] = $matches_eventargument[1];
$postArray['__VIEWSTATE'] = $matches_viewstate[1];
$postArray['__VIEWSTATEENCRYPTED'] = $matches_viewstateencrypted[1];
$postArray['__PREVIOUSPAGE'] = $matches_previouspage[1];
$postArray['ctl00$ddlLanguage'] = 'en';
//$postArray['__EVENTVALIDATION'] = $matches_eventvalidation[1]);
$postArray['ctl00$txtUserName'] = '';
$postArray['ctl00$txtPassword'] = '';
$postArray['ctl00$hfPassword'] = '';
$postArray['ctl00$edtBezeichnung'] = '';
$postArray['ctl00$edtArtikelnr'] = '';
$postArray['ctl00$edtSummenformel'] = '';
$postArray['ctl00$edtCAS'] = '541-59-3';
$postArray['ctl00_ContentPlaceHolder1_ASPxPageControl1ATI'] = '0';
$postvars = "";
foreach($postArray as $key=>$value) { $postvars .= $key.'='.$value.'&'; }
$postvars = rtrim($postvars ,'&');
$fpp=fopen('abcr-tempp.html','a');
fclose($fpp);
$ch = curl_init();
$fpp=fopen('abcr-tempp.html','w');
curl_setopt($ch, CURLOPT_URL, 'http://212.202.102.92/abcrestore/search.aspx');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/abcrcookie.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fpp);
//execute post
$output = curl_exec($ch);
//close connection
curl_close($ch);
fclose($fpp);
感谢您的帮助。
答案 0 :(得分:1)
而不是:
$postvars = "";
foreach($postArray as $key=>$value) { $postvars .= $key.'='.$value.'&'; }
$postvars = rtrim($postvars ,'&');
最好使用http_build_query构建查询字符串:
$postvars = http_build_query($postArray);
检查print_r($postArray)
的输出也是一个好主意,以确保您发送正确的变量:
print_r($postArray);