search
HomeBackend DevelopmentPHP Tutorialphp实现httpclient类示例_php实例

复制代码 代码如下:

httpClient::init($httpClient, $args = null);
$httpClient->get($url, $data = null, $cookie = null);
var_dump($httpClient->buffer);

复制代码 代码如下:

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.php.net/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')

echo $httpClient->buffer;

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用golang中的http.Client进行HTTP请求的高级操作如何使用golang中的http.Client进行HTTP请求的高级操作Nov 18, 2023 am 11:37 AM

如何使用golang中的http.Client进行HTTP请求的高级操作引言:在现代开发中,HTTP请求是不可避免的一部分。golang提供了强大的标准库,其中包含了http包。http包提供了http.Client结构体,用于发送HTTP请求和接收HTTP响应。在本文中,我们将探讨如何使用http.Client进行HTTP请求的高级操作,并提供具体的代码示

使用Java 11中的HttpClient发送HTTP请求并处理响应使用Java 11中的HttpClient发送HTTP请求并处理响应Aug 01, 2023 am 11:48 AM

标题:使用Java11中的HttpClient发送HTTP请求并处理响应引言:在现代的互联网应用程序中,与其他服务器进行HTTP通信是非常常见的任务。Java提供了一些内置的工具,可以帮助我们实现这一目标,其中最新且推荐使用的是Java11中引入的HttpClient类。本文将介绍如何使用Java11中的HttpClient发送HTTP请求并处理响应,

如何使用Java中的httpclient进行重定向和请求转发的比较如何使用Java中的httpclient进行重定向和请求转发的比较Apr 21, 2023 pm 11:43 PM

这里介绍一下:HttpClient4.x版本,get请求方法会自动进行重定向,而post请求方法不会自动进行重定向,这是要注意的地方。我上次发生错误,就是使用post提交表单登录,当时没有自动重定向。请求转发和重定向的区别1、重定向是两次请求,转发是一次请求,因此转发的速度要快于重定向。2、重定向之后地址栏上的地址会发生变化,变化成第二次请求的地址,转发之后地址栏上的地址不会变化,还是第一次请求的地址。3、转发是服务器行为,重定向是客户端行为。重定向时浏览器上的网址改变,转发是浏览器上的网址不变

如何使用Java HttpClient发送HTTP请求如何使用Java HttpClient发送HTTP请求Apr 20, 2023 pm 11:49 PM

1、导入依赖org.apache.httpcomponentshttpclient4.5.3com.alibabafastjson1.2.58org.apache.httpcomponentshttpmime4.5.3org.apache.httpcomponentshttpcore4.4.13org.slf4jslf4j-api1.7.72、使用工具类该工具类将get请求和post请求当中几种传参方式都写了,其中有get地址栏传参、get的params传参、post的params传参、post

如何解决 golang 中的 “undefined: http.Client” 错误?如何解决 golang 中的 “undefined: http.Client” 错误?Jun 24, 2023 pm 05:49 PM

Go语言是一门高效、灵活、并发性强的编程语言,因此被广泛应用于网络编程和并发处理。HTTP客户端是Go语言中比较常用的一个库,但是在使用过程中,如果不小心就会出现“undefined:http.Client”错误。这种错误会给开发者带来不小的困扰,本文将探讨如何解决这个问题。首先,我们需要了解Go语言的导入机制。在Go中,所有的包都需要通过

使用Java 11中的新的HttpClient发送异步HTTP请求并处理响应使用Java 11中的新的HttpClient发送异步HTTP请求并处理响应Jul 31, 2023 pm 02:24 PM

使用Java11中的新的HttpClient发送异步HTTP请求并处理响应在Java11中,新的HttpClient类被引入,提供了强大的功能来发送HTTP请求并处理响应。相比于以往的HttpURLConnection,新的HttpClient更加易于使用,并且支持异步操作,使得处理并发请求更加高效。本文将介绍如何使用Java11中的新的HttpCli

分析Java中HttpClient的错误处理实例分析Java中HttpClient的错误处理实例May 08, 2023 am 11:07 AM

说明1、HttpClient异步请求返回CompletableFuture,其自带的exceptionally方法可用于fallback处理。2、HttpClient不像WebClient那样,它不会出现4xx或5xx的状态码异常,需要根据自己的情况进行处理,手动检测状态码异常或返回其他内容。实例@TestpublicvoidtestHandleException()throwsExecutionException,InterruptedException{HttpClientclient=Ht

使用Java 13中的新的HttpClient发送WebSocket请求和处理响应使用Java 13中的新的HttpClient发送WebSocket请求和处理响应Jul 29, 2023 pm 12:27 PM

使用Java13中的新的HttpClient发送WebSocket请求和处理响应随着Java11的发布,Java平台开始支持原生的WebSocketAPI。然而,在Java13中,新的HttpClientAPI提供了更简单、更易用的方式来发送和处理WebSocket请求。在本篇文章中,我们将学习如何使用Java13中的新的HttpClient发送

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)