php实现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;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Sending HTTP requests and handling responses using HttpClient in Java11 Introduction: In modern Internet applications, HTTP communication with other servers is a very common task. Java provides some built-in tools that can help us achieve this goal. The latest and recommended one is the HttpClient class introduced in Java11. This article will introduce how to use HttpClient in Java11 to send HTTP requests and process responses,

How to use http.Client in golang for advanced operations of HTTP requests Introduction: In modern development, HTTP requests are an inevitable part. Golang provides a powerful standard library, which includes the http package. The http package provides the http.Client structure for sending HTTP requests and receiving HTTP responses. In this article, we will explore how to use http.Client to perform advanced operations on HTTP requests and provide specific code examples.

Here is an introduction: In HttpClient4.x version, the get request method will automatically redirect, but the post request method will not automatically redirect. This is something to pay attention to. The last time I made an error was when I used post to submit the form to log in, and there was no automatic redirection at that time. The difference between request forwarding and redirection 1. Redirection is two requests, and forwarding is one request, so the forwarding speed is faster than redirection. 2. After redirection, the address on the address bar will change to the address requested for the second time. After forwarding, the address on the address bar will not change and remains the address requested for the first time. 3. Forwarding is a server behavior, and redirection is a client behavior. When redirecting, the URL on the browser changes; when forwarding, the URL on the browser remains unchanged.

1. Import the dependency org.apache.httpcomponentshttpclient4.5.3com.alibabafastjson1.2.58org.apache.httpcomponentshttpmime4.5.3org.apache.httpcomponentshttpcore4.4.13org.slf4jslf4j-api1.7.72. Use the tool class. This tool class converts get requests and post requests. Several parameter passing methods have been written, including get address bar passing parameters, get params passing parameters, post params passing parameters, post

Send asynchronous HTTP requests and process responses using the new HttpClient in Java 11 In Java 11, the new HttpClient class was introduced, providing powerful functionality to send HTTP requests and process responses. Compared with the previous HttpURLConnection, the new HttpClient is easier to use and supports asynchronous operations, making it more efficient to handle concurrent requests. This article will introduce how to use the new HttpCli in Java11

Go language is an efficient, flexible and highly concurrency programming language, so it is widely used in network programming and concurrent processing. The HTTP client is a commonly used library in the Go language, but during use, if you are not careful, the "undefined: http.Client" error will appear. This kind of error will cause a lot of trouble to developers. This article will discuss how to solve this problem. First, we need to understand the import mechanism of Go language. In Go, all packages need to pass

Note 1. HttpClient asynchronous request returns CompletableFuture, and its own exceptionally method can be used for fallback processing. 2. Unlike WebClient, HttpClient does not have 4xx or 5xx status code exceptions. You need to handle it according to your own situation, manually detect status code exceptions or return other content. Example@TestpublicvoidtestHandleException()throwsExecutionException,InterruptedException{HttpClientclient=Ht

Use the new HttpClient in Java13 to send WebSocket requests and process responses. With the release of Java11, the Java platform began to support the native WebSocket API. However, in Java 13, the new HttpClient API provides a simpler and easier-to-use way to send and handle WebSocket requests. In this article, we will learn how to use the new HttpClient in Java13 to send
