Home >Backend Development >PHP Tutorial >Share several ways to send HTTP requests in PHP
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, using the curl command line under Linux can 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. This article mainly introduces several ways for PHP to send HTTP requests. In addition to using cURL, PHP sends HTTP requests. Those who are interested can learn more. Hope it helps everyone.
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 streaming 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
##
<?php /** * Created by PhpStorm. * User: tanteng * Date: 2017/7/22 * Time: 13:48 */ function post($url, $data) { $postdata = http_build_query( $data ); $opts = array('http' => 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##
<?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\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); } ?>
Related recommendations:
The above is the detailed content of Share several ways to send HTTP requests in PHP. For more information, please follow other related articles on the PHP Chinese website!