Last active
December 31, 2015 11:59
-
-
Save zhoumengkang/7983333 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * curl实例 | |
| * 第一步:存储cookie | |
| * 第二步:使用cookie,模拟提交查询 | |
| */ | |
| set_time_limit(0);//页面的超时限制 | |
| date_default_timezone_set("PRC"); | |
| define('TIMEOUT',5); | |
| //存cookie | |
| define('COOKIE_FILE',dirname(__FILE__).'./text.txt'); | |
| $code1 = $_POST['code1']; | |
| $code2 = $_POST['code2']; | |
| function mycurl($date){ | |
| if(!$date['url']){ | |
| die('未给定URL参数.'); | |
| } | |
| $ch = curl_init($date['url']); | |
| curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; KB974487)');//在HTTP请求中包含一个"User-Agent: "头的字符串 | |
| curl_setopt($ch, CURLOPT_HEADER,0);//启用时会将头文件的信息作为数据流输出。 | |
| curl_setopt($ch, CURLOPT_AUTOREFERER,true);//当根据Location:重定向时,自动设置header中的Referer:信息。 | |
| if($date['referer']){ | |
| curl_setopt($ch, CURLOPT_REFERER, $date['referer']);//在HTTP请求头中"Referer: "的内容。 | |
| } | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
| curl_setopt($ch, CURLOPT_ENCODING, ' gzip, deflate'); | |
| if($date['header']){ | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $date['header']);//一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置: array('Content-type: text/plain', 'Content-length: 100') | |
| } | |
| if($date['cookie_file']){ | |
| curl_setopt($ch, CURLOPT_COOKIEFILE, $date['cookie_file'] );//包含cookie数据的文件名,cookie文件的格式可以是Netscape格式,或者只是纯HTTP头部信息存入文件。 | |
| curl_setopt($ch, CURLOPT_COOKIEJAR, $date['cookie_file'] );//连接结束后保存cookie信息的文件。 | |
| } | |
| if($date['cookie_str']){ | |
| curl_setopt($ch, CURLOPT_COOKIE, $date['cookie_str'] );//设定HTTP请求中"Cookie: "部分的内容。多个cookie用分号分隔,分号后带一个空格(例如, "fruit=apple; colour=red")。 | |
| } | |
| if($date['post_dates']){ | |
| curl_setopt($ch, CURLOPT_POST, 1);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。 | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $date['post_dates'] );//全部数据使用HTTP协议中的"POST"操作来发送。 | |
| } | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//将 curl_exec() 获取的信息以文件流的形式返回,而不是直接输出。 | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用 CURLOPT_MAXREDIRS 可以限定递归返回的数量。 | |
| $cons = curl_exec($ch); | |
| curl_close($ch); | |
| return $cons; | |
| } | |
| $res1 = mycurl( | |
| array( | |
| 'url'=>'http://58.215.246.158/yxwebquery/', | |
| 'cookie_file'=>COOKIE_FILE, | |
| )); | |
| $html = mycurl(array( | |
| 'url'=>'http://58.215.246.158/yxwebquery/External/ExternalVeh.aspx?PID=02@苏B'.strtoupper($code1).'@02:小型汽车号牌@'.strtoupper($code2), | |
| 'cookie_str'=>COOKIE_FILE, | |
| 'referer'=>'http://58.215.246.158/yxwebquery/Public/WinOpenMain.aspx?PWORK=VEH&PID=02@苏B'.strtoupper($code1).'@02:小型汽车号牌@'.strtoupper($code2), | |
| )); | |
| //正则匹配,重新组装页面 | |
| //var_dump($html); | |
| $pattern = '/<span id="lblTitle" style="color:Red;width:100%;">(.*?)<\/span>/s'; | |
| preg_match_all($pattern,$html,$tip,PREG_SET_ORDER); | |
| echo '<div>'.$tip[0][0].'</div>';//友情提醒: 您有4条曝光未处理,请尽快到窗口处理! | |
| $pattern = '/<table.*?<table.*?>(.*?)<\/table>.*?<div.*?>(.*?)<\/div>.*?<\/table>/s'; | |
| preg_match_all($pattern,$html,$match,PREG_SET_ORDER); | |
| /*下面我把信息单独提取出来,方便你自己再修改样式*/ | |
| //车辆信息 | |
| $info = '<table>'.$match[0][1].'</table>'; | |
| //车辆信息的每个td元素的值 | |
| echo '<pre>'; | |
| $pattern = '/<td.*?>(.*?)<\/td>/'; | |
| preg_match_all($pattern,$info,$matchInfo); | |
| if(trim(strip_tags($matchInfo[0][4]))==""){ | |
| echo '<div>车辆信息输入错误,请重新输入</div>'; | |
| }else{ | |
| foreach($matchInfo[1] as $v){ | |
| $moto_info[] = trim(strip_tags($v),'、'); | |
| } | |
| $info_table2='<table class="table2"><tr class="tr2">'; | |
| foreach($moto_info as $i => $v){ | |
| if( ($i>=0 && $i<4) || ($i>7 &&$i<11)){ | |
| $info_table2 .='<td>'.$v.'</td>'; | |
| } | |
| } | |
| $info_table2 .='</tr><tr>'; | |
| foreach($moto_info as $i => $v){ | |
| if(($i>3 && $i<8)|| ($i>11 && $i<15)){ | |
| $info_table2 .='<td>'.$v.'</td>'; | |
| } | |
| } | |
| $info_table2.='</tr></table>'; | |
| echo $info_table2; | |
| echo '<br/>'; | |
| //违章信息 | |
| $show = $match[0][2]; | |
| preg_match_all($pattern,$show,$baoguang); | |
| $table_bg = '<table class="table2">'; | |
| foreach($baoguang[1] as $i => $v){ | |
| if($i%7==0){ | |
| if($i == 0){ | |
| $table_bg .= '<tr class="tr2"><td>违规编号</td><td>'.$v.'</td>'; | |
| }else{ | |
| $table_bg .= '<tr><td>'.($i/7).'</td><td>'.$v.'</td>'; | |
| } | |
| }elseif($i%7 == 6){ | |
| $table_bg .= '<td>'.$v.'</td></tr>'; | |
| }else{ | |
| $table_bg .='<td>'.$v.'</td>'; | |
| } | |
| } | |
| $table_bg .= '</table>'; | |
| echo $table_bg; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment