Home > Backend Development > PHP Tutorial > Introduction to how php sends HTTP requests

Introduction to how php sends HTTP requests

巴扎黑
Release: 2023-03-15 14:30:01
Original
1673 people have browsed it

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


##

<?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;
}
Copy after login

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template