### Install amphp/http-client Source: https://github.com/amphp/http-client/blob/5.x/README.md Install the amphp/http-client package using Composer. ```bash composer require amphp/http-client ``` -------------------------------- ### Configure Redirect Following Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Control how the client handles HTTP redirects. This example shows how to follow redirects for both 3xx and 2xx status codes on GET or HEAD requests. ```php setAttribute( Client::ATTR_FOLLOW_LOCATION, Client::FOLLOW_LOCATION_ON_3XX | Client::FOLLOW_LOCATION_ON_2XX ); ``` -------------------------------- ### Include Artax Autoloader Source: https://github.com/amphp/http-client/wiki/Getting-Up-and-Running Include the autoloader file from the vendor directory to start using Artax. This is the simplest way to get started. ```php register(); ``` -------------------------------- ### Example HTTP GET Request for Redirected URL Source: https://github.com/amphp/http-client/wiki/An-HTTP-Primer A subsequent HTTP GET request made by a browser after receiving a redirect. ```http GET /domains/example/ HTTP/1.1 User-Agent: Mozilla/123456... Host: www.iana.org Accept: text/html, text/*;q=0.5, */*;q=0.2 Accept-Language: en-us, en;q=0.8, *;q=0.1 ``` -------------------------------- ### Basic PHP Hello World Example Source: https://github.com/amphp/http-client/wiki/An-HTTP-Primer A simple PHP script that outputs 'Hello, world!' and demonstrates PHP's automatic HTTP response construction. ```php ``` -------------------------------- ### Handling Redirection Chains with Artax ClientResponse Source: https://github.com/amphp/http-client/wiki/How-it-Works Iterate through intermediate responses in a redirection chain using `next()` and accessing properties of each response. This example shows how to get the start line and headers of a redirected response. ```php send('http://example.com'); $response->next(); // advance the pointer to the next response in the chain echo $response->getStartLine() . PHP_EOL; foreach ($response->getAllHeaders() as $header) { echo "$header\r\n"; } ``` -------------------------------- ### Perform a Simple GET Request Source: https://github.com/amphp/http-client/wiki/Basic-Client-Usage Use the Artax\Client to send a simple GET request to a URL. The client automatically normalizes common request headers like User-Agent and Host. ```php send('http://example.com'); echo $response->getBody(); } catch (Artax\ClientException $e) { echo $e->getMessage(); } ``` -------------------------------- ### Example HTTP Success Response with Entity Body Source: https://github.com/amphp/http-client/wiki/An-HTTP-Primer A successful HTTP response message containing an HTML entity body. ```http HTTP/1.1 200 OK Date: Thu, 25 Oct 2012 01:09:09 GMT Last-Modified: Wed, 09 Feb 2011 17:13:15 GMT Vary: Accept-Encoding Connection: close Content-Length: 42 Content-Type: text/html; charset=UTF-8 ... blah blah blah ... ``` -------------------------------- ### Example HTTP Redirect Response Source: https://github.com/amphp/http-client/wiki/An-HTTP-Primer An HTTP response message indicating a redirect to a different URL. ```http HTTP/1.0 302 Found Location: http://www.iana.org/domains/example/ Server: BigIP Connection: Keep-Alive Content-Length: 0 ``` -------------------------------- ### Set Request Method Source: https://github.com/amphp/http-client/blob/5.x/README.md Create a Request object and explicitly set its HTTP method to PUT. The default method is GET. ```php $request = new Request("https://httpbin.org/post", "POST"); $request->setBody("foobar"); $request->setMethod("PUT"); ``` -------------------------------- ### Clone Artax Repository Source: https://github.com/amphp/http-client/wiki/Installation Clone the latest Artax source code from GitHub. Use the --recursive option to automatically install the PHP-Datastructures dependency. ```bash $ git clone --recursive git://github.com/rdlowrey/Artax.git ``` -------------------------------- ### Get Response Status and Reason Source: https://github.com/amphp/http-client/blob/5.x/README.md Retrieves the HTTP status code and its associated reason phrase from a response object. ```php $response = $client->request($request); var_dump($response->getStatus(), $response->getReason()); ``` -------------------------------- ### Get Response Protocol Version Source: https://github.com/amphp/http-client/blob/5.x/README.md Retrieves the HTTP protocol version used for the response. ```php $response = $client->request($request); var_dump($response->getProtocolVersion()); ``` -------------------------------- ### Setting and Retrieving Headers Source: https://github.com/amphp/http-client/wiki/On-Headers Demonstrates setting headers with different casing and retrieving them. Note that header names are case-insensitive and setHeader overrides previous values. ```php setHeader('cOnTeNt-LeNgTh', 1); $request->setHeader('Content-Length', 42); var_dump($request->hasHeader('CONTENT-LENGTH')); // bool(true) var_dump($request->getCombinedHeader('content-length')); // 42 foreach ($request->getHeaders('Content-Length') as $header) { echo $header->getValue(); // 42 } ``` -------------------------------- ### Configure HTTP Client with Logging Source: https://github.com/amphp/http-client/blob/5.x/README.md Sets up an HttpClient instance that logs all requests and responses to an HTTP Archive (HAR) file. This is useful for debugging and analysis. ```php use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Client\EventListener\LogHttpArchive; $httpClient = (new HttpClientBuilder) ->listen(new LogHttpArchive('/tmp/http-client.har')) ->build(); $httpClient->request(...); ``` -------------------------------- ### Track Download Speed with EVENT_WRITE Source: https://github.com/amphp/http-client/wiki/Client-Events Listen to the Client::EVENT_WRITE event to track download speed in real-time. This snippet demonstrates how to access the average download speed (KB/s) from the event's statistics. ```php addListener(Client::EVENT_WRITE, function($key, $data, $bytes, $info) { echo "($key) $bytes bytes read -- "; echo "average download speed: " . $info['avgDownKbps'] . ' KB/s' . PHP_EOL; }); ``` -------------------------------- ### Perform a Basic SSL Request Source: https://github.com/amphp/http-client/wiki/Basic-Client-Usage Make an SSL request by configuring the client with a Certificate Authority file path using Client::ATTR_SSL_CA_FILE. This ensures peer certificate verification for secure connections. ```php setAttribute(Client::ATTR_SSL_CA_FILE, CERT_AUTHORITY_FILE); try { $response = $client->send('https://www.bankofamerica.com'); } catch (ClientException $e) { echo $e->getMessage(); } ``` -------------------------------- ### Set Multiple Client Attributes Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Configure several client behaviors simultaneously. This method is efficient for setting multiple configuration options at once, such as connection timeouts and host concurrency limits. ```php setAllAttributes([ Client::ATTR_CONNECT_TIMEOUT => 42, Client::ATTR_HOST_CONCURRENCY_LIMIT => 3 ]); ``` -------------------------------- ### Setting Multiple Client Attributes Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Shows how to set multiple attributes on the HTTP client simultaneously using an associative array. This is efficient for configuring several options at once. ```APIDOC ## Client::setAllAttributes ### Description Sets multiple attributes on the HTTP client instance at once using an associative array. ### Method `setAllAttributes(array $attributes) ### Parameters #### Path Parameters - **attributes** (array) - Required - An associative array where keys are attribute names and values are the desired attribute values. ### Request Example ```php setAllAttributes([ Client::ATTR_CONNECT_TIMEOUT => 42, Client::ATTR_HOST_CONCURRENCY_LIMIT => 3 ]); ``` ``` -------------------------------- ### Build Default HttpClient Source: https://github.com/amphp/http-client/blob/5.x/README.md Build a default HttpClient instance using HttpClientBuilder. This client can be used to make HTTP requests. ```php use Amp\Http\Client\HttpClientBuilder; $client = HttpClientBuilder::buildDefault(); $response = $client->request(new Request("https://httpbin.org/get")); var_dump($response->getStatus()); var_dump($response->getHeaders()); var_dump($response->getBody()->buffer()); ``` -------------------------------- ### Setting Referrer and Cookie Headers with cURL Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Illustrates the cURL method for setting specific request headers like Referrer and Cookie using CURLOPT options. ```php send()`. This allows setting the URI, method, headers, and body. ```php setUri('http://www.example.com'); $request->setMethod('PUT'); $request->setAllHeaders( 'Content-Type' => 'application/json', 'Content-Length' => strlen($myJson), 'Connection' => 'close' ); $request->setBody('{"firstName": "Dan"}'); $response = $client->send($request); ``` -------------------------------- ### Setting Multiple Headers at Once Source: https://github.com/amphp/http-client/wiki/On-Headers Demonstrates using setHeader and setAllHeaders to assign various headers, including an array for multiple values of the same header. Note that setAllHeaders clears existing headers. ```php setHeader('My-Header', 42); $request->setAllHeaders(array( 'Cookie' => array('foo=bar', 'bar=baz'), 'Some-Header' => 'Ford Prefect' )); $request->addHeader('COOKIE', 'someval=mycookie'); ``` -------------------------------- ### Setting a Single Client Attribute Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Demonstrates how to set a single attribute on the HTTP client instance. This is useful for fine-tuning specific behaviors like connection timeouts. ```APIDOC ## Client::setAttribute ### Description Sets a single attribute on the HTTP client instance to modify its behavior. ### Method `setAttribute(string $name, $value) ### Parameters #### Path Parameters - **name** (string) - Required - The name of the attribute to set. - **value** (mixed) - Required - The value to assign to the attribute. ### Request Example ```php setAttribute(Client::ATTR_CONNECT_TIMEOUT, 42); ``` ``` -------------------------------- ### Verify Amphp HTTP Client Requirements Source: https://github.com/amphp/http-client/wiki/Requirements This script checks for the minimum PHP version, the presence of the openssl extension, and ensures that string function overloading is not enabled, which can interfere with the client's operation. It prints an error message if any requirement is not met. ```php intercept(new SetRequestHeader('x-foo', 'bar')) ->intercept(new SetResponseHeader('x-tea', 'now')) ->build(); $response = $client->request(new Request("https://httpbin.org/get")); $body = $response->getBody()->buffer(); ``` -------------------------------- ### Integrate Gzip Encoding with Events Source: https://github.com/amphp/http-client/wiki/Client-Events Use EVENT_REQUEST and EVENT_RESPONSE listeners to automatically handle gzip compression for HTTP transfers. The EVENT_REQUEST listener adds the 'Accept-Encoding: gzip' header, and EVENT_RESPONSE can decompress the body if 'Content-Encoding: gzip' is present. ```php build($eventMediator); // Add gzip to the set of accepted encodings in the request message $eventMediator->addListener(Client::EVENT_REQUEST, function($key, $request) { $request->setHeader('Accept-Encoding', 'gzip, identity'); }); $decompressor = function($response) { if (!$response->hasHeader('Content-Encoding')) { return; } if ('gzip' == $response->getHeader('Content-Encoding')) { $body = $response->getBody(); // decompress gzip-encoded body here $response->setBody($body); } } $eventMediator->addListener( Client::EVENT_RESPONSE, function($key, $response, $stats) use ($decompressor) { $decompressor($response); } ); $request = new StdRequest('http://www.yahoo.com'); $response = $client->send($request); ``` -------------------------------- ### Response Headers Format Source: https://github.com/amphp/http-client/blob/5.x/README.md Illustrates the associative array format for accessing all response headers. ```php [ "header-1" => [ "value-1", "value-2", ], "header-2" => [ "value-1", ], ] ``` -------------------------------- ### Setting Multiple Cookie Headers Source: https://github.com/amphp/http-client/wiki/On-Headers Illustrates setting multiple values for the same header field using an array with setHeader. The output shows each cookie on a new line. ```php setHeader('Cookie', array( 'lu=Rg3vHJZnehYLjVg7qi3bZjzg', 'made_write_conn=1295214458', 'reg_fb_gate=deleted' )); foreach ($request->getAllHeaders() as $header) { echo $header . "\r\n"; } ``` -------------------------------- ### Client::ATTR_SSL_ALLOW_SELF_SIGNED Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Allows the use of self-signed certificates for SSL connections. Requires `Client::ATTR_SSL_VERIFY_PEER` to be `true`. Defaults to `false`. ```APIDOC ## Client::ATTR_SSL_ALLOW_SELF_SIGNED ### Description Allow self-signed certificates. Requires that `Client::ATTR_SSL_VERIFY_PEER` is `true`. ### Type `bool` ### Default Value `false` ``` -------------------------------- ### Assigning Request Methods with Artax Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Shows how to set the HTTP request method (e.g., PUT, HEAD) on an Artax request object using setMethod(). It also highlights the use of Request constants for standard methods. ```php setUri('http://example.com'); $request->setMethod('PUT'); $request->setMethod('HEAD'); // and so on ... ``` ```php setUri('http://example.com'); $request->setMethod(Request::GET); ``` -------------------------------- ### Client::ATTR_SSL_LOCAL_CERT Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Specifies the filesystem path to a local certificate file (PEM-encoded) containing the certificate and private key, potentially including the certificate chain. ```APIDOC ## Client::ATTR_SSL_LOCAL_CERT ### Description A filesystem path pointing to a local certificate. This file must be a PEM-encoded file containing your certificate and private key. The file may optionally contain the certificate chain of issuers. ### Type `string` ### Default Value `null` ``` -------------------------------- ### Sending POST Form Data with cURL Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Demonstrates the cURL approach to sending POST data, which requires setting CURLOPT_POSTFIELDS. ```php send('http://www.google.com'); ``` -------------------------------- ### Set and Add Request Headers Source: https://github.com/amphp/http-client/blob/5.x/README.md Set a specific header field with a value, or add an additional header line without removing existing ones. Multiple headers can also be set at once. ```php $request = new Request("https://httpbin.org/post", "POST"); $request->setHeader("X-Foobar", "Hello World"); $request->setBody("foobar"); ``` -------------------------------- ### Send Multiple HTTP Requests in Parallel Source: https://github.com/amphp/http-client/wiki/Multi-Requests Use `Artax\Client::sendMulti` to send an array of requests concurrently. This is useful for fetching multiple resources at once, optimizing performance by leveraging parallel execution. All exceptions are caught internally, and results are accessed via the returned `ClientMultiResult` object. ```php sendMulti(array( 'google' => 'http://www.google.com', 'yahoo' => 'http://www.yahoo.com', 'bing' => 'http://www.bing.com' )); foreach ($multiResult as $key => $response) { if ($response) { echo $key . ' :: ' . $response->getStartLine() . PHP_EOL; } else { $error = $multiResult->getResult($key); echo "Oh, no! The request at key `$key` encountered the following error: "; echo $error->getMessage(); } } ``` -------------------------------- ### Create and Modify Request Source: https://github.com/amphp/http-client/blob/5.x/README.md Create a new Request object and modify its URI, body, and method. Note that Request objects are mutable. ```php $request = new Request("https://httpbin.org/post", "POST"); $request->setBody("foobar"); $request->setUri("https://google.com/"); ``` -------------------------------- ### Listen for Response Completion Source: https://github.com/amphp/http-client/wiki/Client-Events Add a listener to the EVENT_RESPONSE event to be notified when an individual HTTP response is fully received and populated. This is useful for processing responses in parallel retrieval scenarios. ```php build($eventMediator); $eventMediator->addListener(Client::EVENT_RESPONSE, function($requestKey, $response, $stats) { echo "`$requestKey` completed with status: " . $response->getStatusCode(); }); $multiResult = $client->sendMulti(array( 'google' => 'http://www.google.com', 'yahoo' => 'http://www.yahoo.com', 'microsoft' => 'http://www.microsoft.com' )); ``` -------------------------------- ### Default SSL Peer Verification Settings Source: https://github.com/amphp/http-client/wiki/Transport-Layer-Security Configures the default secure peer verification settings for HTTPS requests. These attributes are applied automatically if not manually overridden. ```php setAllAttributes([ Client::ATTR_SSL_VERIFY_PEER => true, Client::ATTR_SSL_CA_FILE => '', Client::ATTR_SSL_VERIFY_DEPTH => 5, Client::ATTR_SSL_CN_MATCH => $request->getHost() ]); ``` -------------------------------- ### Client::ATTR_SSL_CA_FILE Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Specifies the filesystem path to a certificate authority file used for authenticating the remote peer when `Client::ATTR_SSL_VERIFY_PEER` is enabled. ```APIDOC ## Client::ATTR_SSL_CA_FILE ### Description Location of a certificate authority file on the local filesystem which should be used alongside the `Client::ATTR_SSL_VERIFY_PEER` attribute to authenticate the identity of the remote peer. ### Type `string` ### Default Value `'' (empty string)` ``` -------------------------------- ### Client::ATTR_FOLLOW_LOCATION Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Configures if and how `Location:` headers should be followed for transparent redirection. Defaults to following `3xx` status codes. ```APIDOC ## Client::ATTR_FOLLOW_LOCATION ### Description A bitmask specifying if and how `Location:` headers should be followed for transparent redirection. By default, `Location:` headers for all responses with a `3xx` status code are followed. ### Type `int` ### Default Value `Client::FOLLOW_LOCATION_ON_3XX` ### Allowed Values * `Client::FOLLOW_LOCATION_NONE` * `Client::FOLLOW_LOCATION_ON_3XX` * `Client::FOLLOW_LOCATION_ON_2XX` * `Client::FOLLOW_LOCATION_ON_UNSAFE_METHOD` * `Client::FOLLOW_LOCATION_ALL` ### Example Usage To follow `Location:` headers for all `3xx` and `2xx` responses to `GET` or `HEAD` requests: ```php setAttribute( Client::ATTR_FOLLOW_LOCATION, Client::FOLLOW_LOCATION_ON_3XX | Client::FOLLOW_LOCATION_ON_2XX ); ``` To disable redirection based on the `Location:` header: ```php setAttribute( Client::ATTR_FOLLOW_LOCATION, Client::FOLLOW_LOCATION_NONE ); ``` ``` -------------------------------- ### Set Request Body as String Source: https://github.com/amphp/http-client/blob/5.x/README.md Sets the request body to a string. The library automatically converts strings to HttpContent. ```php $request = new Request("https://httpbin.org/post", "POST"); $request->setBody("foobar"); ``` -------------------------------- ### Client::ATTR_CONNECT_TIMEOUT Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Specifies the number of seconds allowed before a connection attempt times out, resulting in an `Artax\ClientException`. ```APIDOC ## Client::ATTR_CONNECT_TIMEOUT ### Description Number of seconds allowed before a connection attempt times out and an `Artax\ClientException` is thrown. ### Type `int` ### Default Value `60` ``` -------------------------------- ### Listen for Redirect Events Source: https://github.com/amphp/http-client/wiki/Client-Events Attach a listener to the EVENT_REDIRECT event to be notified when a redirect occurs during an HTTP request. This event provides the response that caused the redirection, unlike EVENT_RESPONSE which fires for the final response in a redirect chain. ```php build($eventMediator); $eventMediator->addListener(Client::EVENT_REDIRECT, function($key, $response, $info) { echo "+ REDIRECTING from ($key): " . $response->getRequestUri() . PHP_EOL; }); ``` -------------------------------- ### Adding Another Cookie Header Source: https://github.com/amphp/http-client/wiki/On-Headers Shows how addHeader appends a new value for an existing header field, preserving previous entries. The output includes the newly added header. ```php $request->addHeader('COOKIE', myval=42); foreach ($request->getAllHeaders() as $header) { echo $header . "\r\n"; } ``` -------------------------------- ### Setting Request Headers in Artax Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Shows how to set individual or multiple headers on a request object in Artax using setHeader or setAllHeaders methods. This is the preferred method for header manipulation. ```php setHeader('Referrer', 'http://some-referrer.com'); $request->setHeader('Cookie', 'fruit=apple; colour=red'); ``` ```php setAllHeaders([ 'Referrer' => 'http://some-referrer.com', 'Cookie' => 'fruit=apple; colour=red' ]); ``` -------------------------------- ### Monitor Data Written to Socket Source: https://github.com/amphp/http-client/wiki/Client-Events Use the EVENT_WRITE event to monitor raw HTTP data being sent over the socket. Listeners receive the request key, the data itself, the number of bytes written, and request statistics. ```php addListener(Client::EVENT_WRITE, function($key, $data, $bytes, $stats) { echo $data; }); ``` -------------------------------- ### Send a File as Request Body Source: https://github.com/amphp/http-client/wiki/Basic-Client-Usage Send a file as the request entity body by setting the 'Content-Type' header and passing a stream resource to setBody(). The client automatically handles Content-Length and Transfer-Encoding for streamed bodies. ```php setUri('http://example.com'); $request->setMethod('PUT'); $request->setHeader('Content-Type', 'application/json'); $entityBody = fopen('/path/to/some/file.json'); $request->setBody($entityBody); try { $response = $client->send($request); } catch (Artax\ClientException $e) { echo $e->getMessage(); } ``` -------------------------------- ### Accessing HTTP Response Attributes in Artax Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Demonstrates how to retrieve various attributes from an Artax response object, including status code, headers, and body. Useful for inspecting the full HTTP response. ```php send('http://www.google.com'); echo $response->getStatusCode(); echo $response->getStartLine(); $hasContentLength = $response->hasHeader('Content-Length'); $contentLength = $response->getCombinedHeader('Content-Length'); // 1234 $headers = $response->getAllHeaders(); print_r($headers); echo $response->getRequestUri(); // useful for redirected requests echo $response->getBody(); ``` -------------------------------- ### Client::ATTR_SSL_LOCAL_CERT_PASSPHRASE Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes The passphrase used to encrypt the private key within the certificate file specified by `Client::ATTR_SSL_LOCAL_CERT`. ```APIDOC ## Client::ATTR_SSL_LOCAL_CERT_PASSPHRASE ### Description The passphrase with which the certificate referenced by `Client::ATTR_SSL_LOCAL_CERT` was encoded. ### Type `string` ### Default Value `null` ``` -------------------------------- ### Retrieving Combined Header Values Source: https://github.com/amphp/http-client/wiki/On-Headers Shows how to use getCombinedHeader to retrieve header values, which are comma-concatenated if multiple values exist for the same header field. ```php setAllHeaders(array( 'Cookie' => array('foo=bar', 'bar=baz'), 'Some-Header' => 'Ford Prefect' )); echo $request->getCombinedHeader('cooKIE'); // foo=bar,bar=baz; echo $request->getCombinedHeader('some-HEADER'); // Ford Prefect ``` -------------------------------- ### Custom Autoloader Registration Source: https://github.com/amphp/http-client/wiki/Getting-Up-and-Running Register a custom autoloader function to load Artax classes manually. This provides flexibility in managing class loading. ```php setAttribute(Client::ATTR_CONNECT_TIMEOUT, 42); ``` -------------------------------- ### Client::ATTR_AUTO_REFERER_ON_FOLLOW Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Controls whether the `Referer:` header is automatically set for requests that follow a `Location:` header (redirects). Defaults to `true`. ```APIDOC ## Client::ATTR_AUTO_REFERER_ON_FOLLOW ### Description Automatically set the `Referer:` header in requests that follow a `Location:` header (redirects). ### Type `bool` ### Default Value `true` ``` -------------------------------- ### Working with HTTP Responses Source: https://github.com/amphp/http-client/wiki/How-it-Works Access various aspects of an HTTP response, including status code, headers, and body. The response object can also be iterated to access intermediate responses in a redirection chain. ```php request('http://example.com'); // What was the HTTP status code of the response? echo $response->getStatusCode(); // Does a specific header exist in the response message? If so, what was it's value? if ($response->hasHeader('Date')) { echo $response->getCombinedHeader('Date'); } // Show me all the headers! print_r($response->getAllHeaders()); // Output the full contents of the response entity in string form echo $response->getBody(); // If redirection was used to retrieve the response, what was the final request URI? echo $response->getRequestUri(); // Access the full response for each pit-stop in a redirection chain: foreach ($response as $r) { echo $r->getRequestUri() . PHP_EOL; echo $r->getStartLine() . PHP_EOL; } ``` -------------------------------- ### Client::ATTR_HOST_CONCURRENCY_LIMIT Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Sets the maximum number of simultaneous connections allowed to the same host name. ```APIDOC ## Client::ATTR_HOST_CONCURRENCY_LIMIT ### Description Number of simultaneous connections allowed to the same host name. ### Type `int` ### Default Value `5` ``` -------------------------------- ### Attach Listener to Modify Request Headers Source: https://github.com/amphp/http-client/wiki/Client-Events Attach a listener to the EVENT_REQUEST event to modify request headers before the request is sent. This allows for real-time alteration of request properties after normalization. ```php build($eventMediator); $eventMediator->addListener(Client::EVENT_REQUEST, function($requestKey, $request) { $request->setHeader('User-Agent', 'My Custom User-Agent String'); }); try { $response = $client->send('http://www.google.com'); } catch (ClientException $e) { echo $e; } ``` -------------------------------- ### Sending POST Form Data with Artax Source: https://github.com/amphp/http-client/wiki/Differences-From-cURL Explains how to send POST form data in Artax by setting the Content-Type header and the request body. This method is more direct than cURL's approach. ```php setHeader('Content-Type', 'application/x-www-form-urlencoded'); $request->setBody('param1=val1¶m2=val2'); ``` -------------------------------- ### Client::ATTR_IO_BUFFER_SIZE Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Defines the maximum size in bytes for each discrete read/write operation on a socket. This limits the buffered data during IO operations, not the total request/response size. ```APIDOC ## Client::ATTR_IO_BUFFER_SIZE ### Description Max size in bytes for each discrete read/write operation on a socket. Note that this is not a limit on the size of requests or responses. Instead, it simply limits the amount of data that will be buffered in memory at one time when performing IO operations on a socket stream. ### Type `int` ### Default Value `8192` ``` -------------------------------- ### Disable Redirect Following Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Prevent the client from automatically redirecting requests based on the `Location:` header. This is useful when manual control over redirects is required. ```php setAttribute( Client::ATTR_FOLLOW_LOCATION, Client::FOLLOW_LOCATION_NONE ); ``` -------------------------------- ### Send a Form-Encoded POST Request Source: https://github.com/amphp/http-client/wiki/Basic-Client-Usage Send a POST request with form-encoded data by setting the 'Content-Type' header to 'application/x-www-form-urlencoded' and providing the encoded data in the request body. ```php setUri('http://example.com'); $request->setMethod('POST'); $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $postVars = array( 'foo'=>'bar', 'baz'=>'boom' ); $formEncodedVars = http_build_query($postVars); $request->setBody($formEncodedVars); try { $response = $client->send($request); echo $response->getStartLine(); } catch (Artax\ClientException $e) { echo $e->getMessage(); } ``` -------------------------------- ### Client::ATTR_KEEP_CONNS_ALIVE Source: https://github.com/amphp/http-client/wiki/Setting-Client-Attributes Determines whether connections are kept alive after a response is received. If `true`, connections persist; if `false`, they are closed. ```APIDOC ## Client::ATTR_KEEP_CONNS_ALIVE ### Description If set to `true`, connections will be kept alive unless closed by the remote server. If this attribute is set to `false` all connections will be closed after a response is received. ### Type `bool` ### Default Value `true` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.