Home  >  Article  >  Backend Development  >  Detailed explanation of several ways to send HTTP requests in PHP

Detailed explanation of several ways to send HTTP requests in PHP

黄舟
黄舟Original
2017-07-26 14:59:283208browse

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, but supports a variety of methods for sending HTTP requests. Way.

How PHP sends HTTP requests

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

1.cURL

2.stream stream method

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

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(&#39;http&#39; =>
           array(
             &#39;method&#39; => &#39;POST&#39;,
             &#39;header&#39; => &#39;Content-type: application/x-www-form-urlencoded&#39;,
             &#39;content&#39; => $postdata
           )
  );
  $context = stream_context_create($opts);
  $result = file_get_contents($url, false, $context);
  return $result;
}

3.socket method

Use a socket to establish a connection, splice HTTP messages to send data and make HTTP requests.

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);
}
?>

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

The above is the detailed content of Detailed explanation of several ways to send HTTP requests in PHP. 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