I am trying search products with curl on this page http://212.202.102.92/abcrestore/search.aspx
but with same problems.
First of all I am trying to save cookies and find post variables...viewstate, eventarget, and then I use them with curl
Problem is that I have got no result or better said I have all result. I think problem is with post variables and cookies, but I have no idea what is wrong (maybe all code is wrong??)
Search examples: CAS Number: 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);
Thank you for help.
Instead of this:
$postvars = "";
foreach($postArray as $key=>$value) { $postvars .= $key.'='.$value.'&'; }
$postvars = rtrim($postvars ,'&');
You're better off building your query string with http_build_query:
$postvars = http_build_query($postArray);
It'd be a good idea to check the output of print_r($postArray)
as well, to ensure you're sending the correct variables:
print_r($postArray);
精彩评论