2020-10-29
php使用curl伪造浏览器访问操作示例

php使用curl伪造浏览器访问操作示例

本文实例讲述了php使用curl伪造浏览器访问操作。分享给大家供大家参考,具体如下:原理服务器主要通过User-Agent识别客户端是何种设备User-Agent是Http协议中的一部分,属于头域的组成部分。基本格式为: 浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息。具体方法如下/** * curl获取数据 * @param $url * @return mixed */function get_url($url){ $ifpost = 0; $datafields = ''; $cookiefile = ''; $v = false; //构造随机ip $ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255 array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255 array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255 array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255 array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255 array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255 array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255 array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255 array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255 ); $rand_key = mt_rand(0, 9); $ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));//模拟http请求header头 $header = array("Connection: Keep-Alive","Accept: text/html, application/xhtml+xml, */*", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3","User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)",'CLIENT-IP:'.$ip,'X-FORWARDED-FOR:'.$ip); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, $v); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $ifpost && curl_setopt($ch, CURLOPT_POST, $ifpost); $ifpost && curl_setopt($ch, CURLOPT_POSTFIELDS, $datafields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $cookiefile && curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); $cookiefile && curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); curl_setopt($ch,CURLOPT_TIMEOUT,60); //允许执行的最长秒数 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $ok = curl_exec($ch); curl_close($ch); unset($ch); return $ok;}更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》希望本文所述对大家PHP程序设计有所帮助。...
2020-10-28
PHP CURL实现模拟登陆并上传文件操作示例

PHP CURL实现模拟登陆并上传文件操作示例

本文实例讲述了PHP CURL实现模拟登陆并上传文件操作。分享给大家供大家参考,具体如下:<?phpheader('content-type:text/html;charset=gb2312');//要注意你需要上传的网站服务器的运行环境,还要看它的请求是否被压缩和转码还有就是//在框架中或者说php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函数来转化文件了//注意你要发送的服务器的header头的结构和特殊参数,实在不行就自己构建一个。废话不多说,直接上代码。function curl_form($post_data,$sumbit_url,$http_url,$cookie_file){ $headers = array(); $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; $headers[] = 'Cache-Control: max-age=0'; $headers[] = 'Accept-Encoding: gzip, deflate'; $headers[] = 'Origin: http://my.***.com'; $headers[] = 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'; $headers[] = 'Upgrade-Insecure-Requests: 1'; $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'; $headers[] = 'Connection: keep-alive';// $headers[] = 'Cookie: ASPSESSIONIDCCTCTQQC=KBGLPDKBIKDIDCBGFOKNMKOE'; //初始化 $ch = curl_init(); //设置变量 curl_setopt($ch, CURLOPT_URL, $sumbit_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示 curl_setopt($ch, CURLOPT_REFERER, $http_url); //表单数据,是正规的表单设置值为非0 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');// curl_setopt($ch, CURLOPT_ENCODING, "");// curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); //执行并获取结果 $output = curl_exec($ch); if($output === FALSE) { echo "<br/>","cUrl Error:".curl_error($ch); }else{ return $output; } // 释放cURL句柄 curl_close($ch);}$temp = array();$temp['title'] = iconv( "utf-8", "gb2312//IGNORE" , "牛排店加盟");$temp['ly'] = iconv( "utf-8", "gb2312//IGNORE" , "特色餐饮加盟");$temp['classid'] = iconv( "utf-8", "gb2312//IGNORE" , "7159");$temp['newssort'] = iconv( "utf-8", "gb2312//IGNORE" , "1");$temp['panduan'] = iconv( "utf-8", "gb2312//IGNORE" , "0");$temp['submit_button'] = iconv( "utf-8", "gb2312//IGNORE" , "发布");$temp['addr'] = iconv( "utf-8", "gb2312//IGNORE" , "bjcanyin");$temp['ContentBg'] = "";$temp['newss'] = iconv( "utf-8", "gb2312//IGNORE" , htmlspecialchars_decode("<p>阿会计师的贺卡收到框架</p><p><img alt=\"\" src=\" http://localhost/super/Uploads/img/2017-08-10/598c145a9527e.jpg\" style=\"height:243px; width:324px\" /></p>", ENT_QUOTES));$cookie_file = dirname(__FILE__)."/jdzj.tmp";$sumbit_url = "http://***/news/***.asp";$http_url="http://***/news/***.asp?act=addok";$img = curl_form($temp,$http_url,$sumbit_url,$cookie_file);var_dump($img);PS:关于PHP curl选项详细说明可参考https://www.jb51.net/article/39331.htm更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》希望本文所述对大家PHP程序设计有所帮助。...
2020-10-27
PHP如何使用cURL实现Get和Post请求

PHP如何使用cURL实现Get和Post请求

先来看一下在PHP中建立cURL请求的基本步骤:  (1)初始化    curl_init()  (2)设置变量    curl_setopt() 。最为重要。有一长串cURL参数可供设置,它们能指定URL请求的各个细节。要一次性全部看完并理解可能比较困难,所以今天我们只试一下那些更常用也更有用的选项。  (3)执行并获取结果    curl_exec()  (4)释放cURL句柄    curl_close()下面就看一下具体的实现:1.Post方式实现(模拟Post请求,调用接口)<?php$url = "http://192.168.147.131/index.php/addUser";//你要请求的地址$post_data = array( "uid" => "1111", "username" => "lunar", "nickname" => "吾独望月",);$ch = curl_init();//初始化cURL curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串并输出到屏幕上curl_setopt($ch,CURLOPT_POST,1);//Post请求方式curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);//Post变量 $output = curl_exec($ch);//执行并获得HTML内容curl_close($ch);//释放cURL句柄 print_r($output);2.Get方式实现<?php$url = "http://www.cnblogs.com/blogforly/";//你要请求的地址 $ch = curl_init();//初始化cURL curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串并输出到屏幕上curl_setopt($ch, CURLOPT_HEADER, 0);//设置header $output = curl_exec($ch);//执行并获得HTML内容curl_close($ch);//释放cURL句柄 print_r($output);以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持梦主博客。...
微信二维码