php curl cookie 存取示例 好多人发来消息询问curl存取cookie文件的问题,杜工并不觉得这是个难点,因为只看手册就可以很容易把握。下面给个例子,看完后就全都明了了:
<?php $cookie_jar_index = dir(__FILE__)."/".'cookie.txt'; $url = "http://www./perl/login.pl"; $params = "username=dudu&password=****"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_index); //curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple; colour=red"); //上面代码是直接传递cookie信息,而非文件 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //curl_setopt($ch, CURLOPT_NOBODY, 1);//这个不能打开,否则无法生成cookie文件 ob_start(); curl_exec($ch); curl_close($ch); ob_clean(); $url = "http://www./perl/myfavorites.pl"; $ch2 = curl_init(); curl_setopt($ch2, CURLOPT_URL, $url); curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar_index); ob_start(); curl_exec($ch2); curl_close($ch2); $rs = ob_get_contents(); //$rs就是返回的内容 ob_clean(); print_r($rs); ?> CURL IP伪装,更改IP头信息
<?php $ip = rand(1,255).".".rand(1,255).".".rand(1,255).".".rand(1,255).""; $ip='8.8.8.8'; $ch = curl_init(); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1); curl_setopt($ch,CURLOPT_HTTPHEADER,array("Client_Ip: ".$ip."", "Real_ip: ".$ip."", "X_FORWARD_FOR: ".$ip."", "X-forwarded-for: ".$ip."", "PROXY_USER: ".$ip."")); curl_setopt($ch, CURLOPT_URL,"http://192.168.1.99/11.php"); echo curl_exec($ch); curl_close($ch); ?> 高级篇: CURLOPT_PROXY这个参数是否真的使用代理去请求数据?那他跟伪装IP的区别有哪些?下面我们做个试验。 curl_proxy_test.php <?php define ('IS_PROXY', true); //是否启用代理 /* cookie文件 */ $cookie_file = dirname ( __FILE__ ) . "/cookie_" . md5 ( basename ( __FILE__ ) ) . ".txt"; // 设置Cookie文件保存路径及文件名 /*模拟浏览器*/ $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; $proxy = "61.135.169.125"; function vlogin($url, $data) { // 模拟登录获取Cookie函数 $curl = curl_init (); // 启动一个CURL会话 if (IS_PROXY) { //以下代码设置代理服务器 //代理服务器地址 curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] ); } // IP伪装 $ip = rand(1,255).".".rand(1,255).".".rand(1,255).".".rand(1,255).""; curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip)); //构造IP curl_setopt($curl, CURLOPT_REFERER, "http://www./ "); //构造来路 curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址 curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查 curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在 curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 模拟用户使用的浏览器 @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转 curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求 curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包 curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS ['cookie_file'] ); // 存放Cookie信息的文件名称 curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 读取上面所储存的Cookie信息 curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环 curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容 curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec ( $curl ); // 执行操作 if (curl_errno ( $curl )) { echo 'Error ' . curl_error ( $curl ); } curl_close ( $curl ); // 关闭CURL会话 return $tmpInfo; // 返回数据 } $params = "username=dudu&password=****"; $result = vlogin("http://demo2./curl_proxy_test2.php",$params); echo $result; ?> curl_proxy_test2.php <?php error_reporting("E_ALL ^ (E_NOTICE | E_WARNING)"); function getClientIp() { if (!empty($_SERVER["HTTP_CLIENT_IP"])) $ip = $_SERVER["HTTP_CLIENT_IP"]; else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; else if (!empty($_SERVER["REMOTE_ADDR"])) $ip = $_SERVER["REMOTE_ADDR"]; else $ip = "err"; return $ip; } echo "<br />IP: " . getClientIp() . "<br />"; print_r($_SERVER); ?> 执行http://demo2./curl_proxy_test.php 输出内容为:Error couldn't connect to host 表明你必须要有台代理服务器才能完成操作。说到伪装跟代理的区别时,伪装会被识破的,只有使用匿名代理才能达到真正代理的效果。
|
|