首页 > php教程 > PHP源码 > 正文

采集远程网址数据

PHP中文网
发布: 2016-05-25 16:58:48
原创
1276人浏览过

php代码

<?php
/**
 * 采集远程网址数据
 */
class Get_Remote_data
{
	/**
	 * 从远程网址采集数据
	 * @param string $uri 远程地址
	 * @param string $rand_ip 随机ip
	 * @param string $user_agent 模拟的user_agent
	 * @param string $referer_uri 模拟的来路的地址
	 * @param array $post_data 需要post的数据,默认为空数组
	 * @param string $method 选择数据提交的数据,默认get
	 * @return mixed
	 */
	public function get_data_from_remote($uri, $rand_ip, $user_agent, $referer_uri='', $post_data=array(), $method="get")
	{
		$ch = curl_init();
	
		curl_setopt($ch, CURLOPT_URL, $uri);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$rand_ip, 'CLIENT-IP:'.$rand_ip));
		//追踪返回302状态码,继续抓取
		// curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	
		if($method == 'post') {
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
	
		curl_setopt($ch, CURLOPT_NOBODY, false);
	
		if($method == 'get') {
			curl_setopt($ch, CURLOPT_REFERER, $referer_uri);//模拟来路
		}
	
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
	
		$data = curl_exec($ch); # 获取数据
		$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE); # 获取http状态码
	
		curl_close($ch);
	
		return array(
				'http_status' => $http_code,
				'data' => $data
		);
	}
	   
    /**
     * 生成模拟来路地址
     * @return string
     */
    private function get_refer_uri()
    {
    	$refer[0] = 'http://www.baidu.com';
    	$refer[1] = 'https://www.google.com';
    
    	$i = time() % count($refer);
    	
    	return $refer[$i];
    }
    
    /**
     * 生成agent
     * @return string
     */
    private function get_user_agent()
    {
    	// windows + chrome
    	$str[0] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
                    (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
    	
    	// windows + ie
    	$str[1] = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; MASPJS; rv:11.0) like Gecko";
    
    	$i = time() % count($str);
    	
    	return $str[$i];
    }
    
    /**
     * 生成随机ip
     * @return string
     */
    private function get_rand_ip()
    {
    	$arr_1 = array("218","218","66","66","218","218","60","60","202","204",
    			"66","66","66","59","61","60","222","221","66","59","60",
    			"60","66","218","218","62","63","64","66","66","122","211");
    
    	$randarr= mt_rand(0,count($arr_1));
    	$ip1id = $arr_1[$randarr];
    
    	$ip2id=   round(rand(600000,   2550000)   /   10000);
    	$ip3id=   round(rand(600000,   2550000)   /   10000);
    	$ip4id=   round(rand(600000,   2550000)   /   10000);
    
    	return   $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
    }
    
}
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号