阅读 96

爬虫 | php封装 | file_get_contents

标签:eth   验证   方便   res   今天   ade   get   ext   rom   

今天无聊,用php封装了一套比较简单的http请求类
细节方面可以再优化

class Creeper
{
	public $url;
	public $header;
	public $text;
	public $responseHeader;
	public function __construct($url){
		$this->url = $url;
		$this->header = "Accept-language: *\r\n" .
	    	"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36\r\n"
	    	;
	}
	public function get(){
		$a = array(
			‘http‘ => array(
				‘method‘=> "GET",
		    	‘header‘=> $this->header
  			),
  			‘ssl‘ => array(    # 取消ssl验证
  				"verify_peer" => false,
  				"verify_peer_name" => false
  			)
		);
		$context = stream_context_create($a);     // 创建上下文
		$url = $this->url;
		$this->text = file_get_contents($url, false, $context);    // 进行访问
		$this->responseHeader = $http_response_header;
		//var_dump($http_response_header);
	}
	public function post($data){
		$postdata = urlencode($data);
		$a = array(
			‘http‘=>array(
				‘method‘=> "POST",
		    	‘header‘=> $this->header.
		    		"Content-Type: application/x-www-urlencoded\r\n",
		    	‘content‘ => $postdata
  			)
		);
		$context = stream_context_create($a);     // 创建上下文
		$url = $this->url;
		$this->text = file_get_contents($url, false, $context);    // 进行访问
		$this->responseHeader = $http_response_header;
		//var_dump($http_response_header);
	}
}

基本上使用起来也还是挺方便的:

$creeper = new Creeper(‘http://www.xxxxx.com‘);
$creeper->get();
preg_match_all("/href=\"(.*?)\" target=/", $creeper->text, $links);
$links = $links[1];

over.

爬虫 | php封装 | file_get_contents

标签:eth   验证   方便   res   今天   ade   get   ext   rom   

原文地址:https://www.cnblogs.com/Mz1-rc/p/14477125.html


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐