Several Methods for sending HTTP requests by PHP, and a discussion on sending requests by php

Source: Internet
Author: User

Several Methods for sending HTTP requests by PHP, and a discussion on sending requests by php

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

CURL is a tool for data transmission and supports multiple protocols. For example, you can use curl command lines in Linux to send various HTTP requests. PHP cURL is an underlying library that can communicate with various servers based on different protocols. HTTP is one of them.

A modern PHP development framework usually uses a package called GuzzleHttp. It is an HTTP client and can also be used to send various HTTP requests. What is its implementation principle, what is the difference with cURL?

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 FAQ of GuzzleHttp. It can be seen that GuzzleHttp does not rely on the cURL library, but supports multiple HTTP request sending methods.

How PHP sends an HTTP request

In addition to cURL, PHP sends HTTP requests.

1. cURL

Detailed Method: http://www.bkjia.com/article/56492.htm

2. stream Mode

Stream_context_create: Creates and returns a text data stream and applies various options. It can be used for fopen (), file_get_contents () special procedures for timeout settings, proxy server, request method, and 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('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 to PHP stream article: http://www.bkjia.com/article/68891.htm

3. socket mode

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

Example of a 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 describes several different methods for sending HTTP requests.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.