#!/usr/local/php/bin/php <?php $runtime = new runtime (); $runtime->start (); $cookie_jar = tempnam('/tmp','cookie');
$filename = $argv[1]; $start_num= $argv[2]; $end_num = $argv[3];
for($i=$start_num; $i<$end_num; $i++){ $zip = sprintf('6s',$i);
$fields_post = array( 'postcode' => $zip, 'queryKind' => 2, 'reqCode' => 'gotoSearch', 'search_button.x'=>37, 'search_button.y'=>12 );
$fields_string = http_build_query ( $fields_post, '&' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "URL?reqCode=gotoSearch&queryKind=2&postcode=".$zip); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120 ); curl_setopt($ch, CURLOPT_REFERER, $refer ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login ); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar ); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar ); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_POST, 1); // 發送一個常規的Post請求 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string );
$data = curl_exec($ch); preg_match_all('/id="table1">[s]*?<tr>[s]*?<td class="maintext">[sS]*?</td>[s]*?</tr>/', $data, $matches); if (!$handle = fopen($filename, 'a+')) { echo "不能開啟檔案 $filename"; echo "n"; exit; }
if (fwrite($handle, $matches[0][1]) === FALSE) { echo "不能寫入到檔案 $filename"; echo "n"; exit; }
echo "成功地將 $somecontent 寫入到檔案$filename"; echo "n";
fclose($handle); curl_close($ch); }
class runtime { var $StartTime = 0; var $StopTime = 0; function get_microtime() { list($usec,$sec)=explode(' ',microtime());return((float)$usec+(float)$sec); } function start() { $this->StartTime=$this->get_microtime(); } function stop(){ $this->StopTime=$this->get_microtime(); } function spent() { return ($this->StopTime-$this->StartTime); } }
$runtime->stop ();
$con = 'Processed in'.$runtime->spent().'seconds'; echo 'Processed in'. $runtime->spent().'seconds';
|