找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 549|回复: 0
打印 上一主题 下一主题

php实现httpclient类示例

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 05:59:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            [U]复制代码[/U] 代码如下:
httpClient::init($httpClient, $args = null);
$httpClient->get($url, $data = null, $cookie = null);
var_dump($httpClient->buffer);
[U]复制代码[/U] 代码如下:
class httpClient {
public $buffer = null;  // buffer 获取返回的字符串
public $referer = null;  // referer 设置 HTTP_REFERER 的网址
public $response = null; // response 服务器响应的 header 信息
public $request = null;  // request 发送到服务器的 header 信息
private $args = null;
public static function init(&$instanceof, $args = array()) {
  return $instanceof = new self($args);
}
private function __construct($args = array()) {
  if(!is_array($args)) $args = array();
  $this->args = $args;
  if(!empty($this->args['debugging'])) {
   ob_end_clean();
   set_time_limit(0);
   header('Content-Type: text/plain; charset=utf-8');
  }
}
public function get($url, $data = null, $cookie = null) {
  $parse = parse_url($url);
  $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
  $host = $parse['host'];
  $header  = 'Host: '. $host. "\r\n";
  $header .= 'Connection: close'. "\r\n";
  $header .= 'Accept: */*'. "\r\n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  $options = array();
  $options['http']['method'] = 'GET';
  $options['http']['header'] = $header;
  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);
}
public function post($url, $data = null, $cookie = null) {
  $parse = parse_url($url);
  $host = $parse['host'];
  $header  = 'Host: '. $host. "\r\n";
  $header .= 'Connection: close'. "\r\n";
  $header .= 'Accept: */*'. "\r\n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  $header .= 'Content-Type: application/x-www-form-urlencoded'. "\r\n";
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  if($data) $header .= 'Content-Length: '. strlen($data). "\r\n";
  $options = array();
  $options['http']['method'] = 'POST';
  $options['http']['header'] = $header;
  if($data) $options['http']['content'] = $data;
  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);
}
}
httpClient::init($httpClient, array( 'debugging' => true , 'userAgent' => 'MSIE 15.0' ));
$httpClient->get('http://www.baidu.com', 'name=haowei');
echo $httpClient->request; // 获取 请求头部信息
echo $httpClient->response; // 获取 响应的头部信息
echo $httpClient->buffer; // 获取 网页内容
$httpClient->get('http://www.jb51.net/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')
echo $httpClient->buffer;
            
            
您可能感兴趣的文章:
  • php cli 方式 在crotab中运行解决
  • 关于nginx+php5.3.8+eclipse3.7工作空间的配置方法
  • Eclipse中php插件安装及Xdebug配置的使用详解
  • php cli模式学习(PHP命令行模式)
  • php-cli简介(不会Shell语言一样用Shell)
  • 浅谈Eclipse PDT调试PHP程序
  • Eclipse的PHP插件PHPEclipse安装和使用
  • php cli配置文件问题分析
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端