php 利用curl 模拟浏览器请求

<?php

class request
{
	private $curl;
	private $query = '';
	private $user_agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36';

	public function __construct()
	{
		$this->curl = curl_init(); // 初始化
	}

	public function request($url, $data = [], $type = 'GET')
	{
		$ret= '';

		switch ($type)
		{

			case 'GET';
				$ret = $this->get($url, $data);
			break;

			case 'POST';
				$ret = $this->post($url, $data);
			break;

		}

		return $ret;
	}

	// 模拟浏览器get请求
	public function get($url, $data)
	{
		if ($data) {
			foreach ($data as $key => $value) {
				$this->query .= $key . '=' . $value;
			}

			$url .= '?' . $this->query;
		}

        curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);//代理服务器地址
	    curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
	    curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	    curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
	    curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
	    @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
	    curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	    curl_setopt($this->curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
	    curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
	    curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
	    curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
	    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
	    $tmpInfo = curl_exec($this->curl); // 执行操作
	   	
		return $tmpInfo;
	}

	// 模拟浏览器post请求
	public function post($url, $data)
	{
        //代理服务器地址
        curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);
    
	    curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
	    curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
	    curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
	    curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
	    @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
	    curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	    curl_setopt($this->curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
	    curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
	    curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
	    curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
	    curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
	    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
	    $tmpInfo = curl_exec($this->curl); // 执行操作
	   
		return $tmpInfo;
	}

	public function __destruct()
	{
		//关闭URL请求
		curl_close($this->curl);
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值