OpenSwoole Coroutine HTTP/2 Client Request Object

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5


Description

When sending a request using the HTTP/2 Client it expects a OpenSwoole\Http2\Request object, you build up this object before you call send(), this is what the client will use when sending the request to the remote host, based on the settings within the object given.

The OpenSwoole\Http2\Request object does not have any methods, only public class properties which you can set to build a request you want to send.


The request object is built up of the following:

<?php

object(OpenSwoole\Http2\Request)#3 (6) {
  ["path"]=>
  string(1) "/"
  ["method"]=>
  string(3) "GET"
  ["headers"]=>
  NULL
  ["cookies"]=>
  NULL
  ["data"]=>
  string(0) ""
  ["pipeline"]=>
  bool(false)
}

All the above class properties are public, meaning you can set values by doing for example: $request->path = '/home/account'. You should use this object to build up a request and send it off using send().


Class Properties

  • path: Is a string where you set the URL you want to send a request to. It must begin with a / and not need to include the protocol or domain, just the URL path like /home/user/account for example. Or /index/php?a=2&b=2.

  • method: Is a string which contains the HTTP request type you are sending, so this could be a GET, POST, PUT etc.

  • headers: This property is a key-value array of headers you want to send during the request.

  • cookies: Is a key-value array of cookie data to send during the request.

  • data: Is a property which is what will be used as the HTTP request body. If a string is used, this will just be placed as the body content. If an array is used, the client will automatically convert it using the x-www-form-urlencoded format, the Content-Type header will be set as well.

  • pipeline: Is a boolean used to decide if you want to keep the stream open or not, if set to true it allows you to continue writing to the stream. Default is false, so the stream is closed for you. For more information and an example see the write() method.


Example

Check the main Quick Start Examples to see how the request object is used in its full context. But the following is a general example:


<?php

  // Create and build a request that will be sent...
  $request = new OpenSwoole\Http2\Request();

  // Set the URL path
  $request->path = "/index.php";

  // Setup request headers to use
  $request->headers = [
      'host' => "localhost",
      "user-agent" => 'Chrome/49.0.2587.3',
      'accept' => 'text/html,application/xhtml+xml,application/xml',
      'accept-encoding' => 'gzip',
  ];

  // Add cookies to the request
  $request->cookies = ['name' => 'hello', 'email' => '[email protected]'];

  ...
Last updated on September 1, 2022