Curl实例分析
请查看这个网址http://www.ifengyin.com/ 看一下左边部分查询有一个图片的验证码。同时原网站是:http://www.xianjj.com/左边的查询。
要查询这个就必须先破解原网站这个验证码生成的机制或cookie。
/** * CURL 获取内容 * @param $durl * @param string $refer * @param bool $get_header * @param bool $hide_body * @param bool $cookie * @return mixed */ function curl_get_contents($durl, $refer = “”, $get_header = false, $hide_body = true, $cookie = false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $durl); curl_setopt($ch, CURLOPT_TIMEOUT, 20); if ($get_header) { curl_setopt($ch, CURLOPT_HEADER, 1); //获取Header if ($hide_body) { curl_setopt($ch, CURLOPT_NOBODY, true); //Body就不要了吧,我们只是需要Head } } if ($refer != “”) { $header = array(“Referer: {$refer}”); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $r = curl_exec($ch); curl_close($ch); return $r; } //获取验证码并保存cookie //获取文件流与cookie if (!empty($_GET['code']) && $_GET['code'] == “getimage”) { header(‘Content-type: image/jpeg’); $url = “http://117.36.53.122:9081/wfcx/imageServlet”; $ch = curl_init($url); //初始化 curl_setopt($ch, CURLOPT_HEADER, 1); //将头文$related_goods件的信息作为数据流输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流 $content = curl_exec($ch); //执行curl并赋值给$content curl_close($ch); preg_match(‘/Set-Cookie:(.*);/iU’, $content, $str); //正则匹配 $data = explode(“rnrn”, $content, 2); //设置SESSION $_SESSION['remote_session_id'] = $str[1]; echo $data[1]; exit; } //页面输入验证码 $jdccode = $_GET['jdccode']; //验证码 $cookie = $_SESSION['remote_session_id']; if (!$cookie) { exit(‘<script>alert(“请重新输入验证码!”);history.back();</script>’); } unset($_SESSION['remote_session_id']); $url = “http://117.36.53.122:9081/wfcx/query.do?actiontype=vioSurveil” . “&hpzl={$hpzl}&hphm={$hphm}&tj={$tj}&tj_val={$tj_val}&jdccode={$jdccode}”; $result = curl_get_contents($url, “”, false, false, $cookie);//获取远程内容
后面就只是拆分网页了!