Home  >  Article  >  Backend Development  >  Introduction to how php sends HTTP requests

Introduction to how php sends HTTP requests

巴扎黑
巴扎黑Original
2017-08-23 11:15:291496browse

This article mainly introduces several ways in which PHP sends HTTP requests, and sorts out the ways in which PHP sends HTTP requests in addition to using cURL. Those who are interested can learn more.

In PHP development, we often use cURL to encapsulate HTTP requests. What is cURL?

cURL is a tool used to transmit data and supports multiple protocols. For example, you can use the curl command line under Linux to send various HTTP requests. PHP's cURL is a low-level library that can communicate with various servers according to different protocols, and the HTTP protocol is one of them.

A package is often used in modern PHP development frameworks, called GuzzleHttp. It is an HTTP client and can also be used to send various HTTP requests. So what is its implementation principle? It is related to cURL Why is it different?

Does Guzzle require cURL?

No. Guzzle can use any HTTP handler to send requests. This means that Guzzle can be used with cURL, PHP's stream wrapper, sockets , and non-blocking libraries like React. You just need to configure an HTTP handler to use a different method of sending requests.

This is a question in the GuzzleHttp document FAQ. It can be seen that GuzzleHttp does not rely on the cURL library. And supports multiple ways of sending HTTP requests.

How PHP sends HTTP requests

So here is a summary of the ways PHP sends HTTP requests besides using cURL.

1.cURL

Detailed method: http://www.jb51.net/article/56492.htm

2. Stream stream method

stream_context_create Function: Create and return a text data stream and apply various options, which can be used for timeout settings, proxy servers, request methods, etc. of processes such as fopen(), file_get_contents(), etc. A special process for setting header information.

Take a POST request as an example:

PHP


##


           array(
             'method' => 'POST',
             'header' => 'Content-type: application/x-www-form-urlencoded',
             'content' => $postdata
           )
  );
  $context = stream_context_create($opts);
  $result = file_get_contents($url, false, $context);
  return $result;
}

Introduction article about PHP stream: http ://www.jb51.net/article/68891.htm

3.socket method

Use a socket to establish a connection and splice HTTP messages to send data. HTTP request.

An example of GET method:

PHP

##

\n";
} else {
  $out = "GET / HTTP/1.1\r\n";
  $out .= "Host: www.example.com\r\n";
  $out .= "Connection: Close\r\n\r\n";
  fwrite($fp, $out);
  while (!feof($fp)) {
    echo fgets($fp, 128);
  }
  fclose($fp);
}
?>

This article introduces several methods of sending HTTP requests different way.

The above is the detailed content of Introduction to how php sends HTTP requests. For more information, please follow other related articles on the PHP Chinese website!

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