### Create a Basic HTTP Request with EasyRequest Source: https://github.com/shtse8/easyrequest/blob/master/README.md Demonstrates how to initialize a new request instance using `\EasyRequest\Client::request()`. It shows examples for GET and POST methods, and how to send the request immediately or in a chained manner. ```php $request = \EasyRequest\Client::request('https://google.com'); $request = \EasyRequest\Client::request('https://google.com', 'GET', $options); $request = \EasyRequest\Client::request('https://google.com', 'POST', $options); // then $request->send(); // or $request = \EasyRequest\Client::request('https://google.com')->send(); ``` -------------------------------- ### Install EasyRequest with Composer Source: https://github.com/shtse8/easyrequest/blob/master/README.md This snippet shows how to install the EasyRequest library using Composer, the PHP dependency manager. It adds the `ptcong/easyrequest` package to your project, making it available for use. ```php composer require ptcong/easyrequest:^1.0 ``` -------------------------------- ### Perform Quick HTTP Requests with EasyRequest Static Methods Source: https://github.com/shtse8/easyrequest/blob/master/README.md Explains how to use static helper methods like `get()`, `post()`, `head()`, `patch()`, and `delete()` for immediate request sending. These methods create and send the request in a single call, eliminating the need for an explicit `send()` call. ```php $response = \EasyRequest\Client::get('https://google.com', 'GET', array( 'follow_redirects' => 3, 'query' => 'a=b&c=d' )); $response = \EasyRequest\Client::post('https://google.com', ...); $response = \EasyRequest\Client::head('https://google.com', ...); $response = \EasyRequest\Client::patch('https://google.com', ...); $response = \EasyRequest\Client::delete('https://google.com', ...); ... more ``` -------------------------------- ### Configure Proxy for EasyRequest PHP Client Source: https://github.com/shtse8/easyrequest/blob/master/README.md Demonstrates how to configure HTTP, SOCKS4, SOCKS4A, and SOCKS5 proxies for requests using the EasyRequest\Client. It shows both initial request setup and chaining methods for proxy configuration, including user credentials. ```php $request = \EasyRequest\Client::request('http://domain.com', 'POST', array( 'proxy' => '192.168.1.105:8888', 'proxy_userpwd' => 'user:pass', 'proxy_type' => Client::PROXY_SOCKS5, // if not given, it will use this proxy as HTTP_PROXY )); $request->withProxy('192.168.1.105:8888', 'user:pass', Client::PROXY_SOCKS5); $request->withProxy('192.168.1.105:8888', null, Client::PROXY_SOCKS4); $request->withProxy('192.168.1.105:8888', null, Client::PROXY_HTTP); $request->withSocks4Proxy('192.168.1.105:8888'); $request->withSocks5Proxy('192.168.1.105:8888', 'user:pass'); $request->withHttpProxy('192.168.1.105:8888', 'user:pass'); ``` -------------------------------- ### Add Custom Headers to EasyRequest HTTP Requests Source: https://github.com/shtse8/easyrequest/blob/master/README.md Illustrates two ways to add custom HTTP headers to a request: by including them in the initial options array or by using the `withHeader()` method. It shows examples for common headers like User-Agent and Cookie, and how to add multiple values for a single header. ```php $request = \EasyRequest\Client::request('https://google.com', 'GET', array( 'header' => array( 'User-Agent' => 'Firefox 45', 'Cookie' => 'cookie1=value1; cookie2=value2;', 'Example' => array( 'value 1', 'value 2' ) ) )); // you also can use PSR7 as $request = \EasyRequest\Client::request('https://google.com') ->withHeader('User-Agent', 'Firefox 45') ->withHeader('Example', array('value1', 'value 2')); ``` -------------------------------- ### Retrieve All Redirected Requests in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Explains how to get a collection of all intermediate request objects when the 'follow_redirects' option is active, with each object conforming to PSR-7. ```php var_dump($request->getRequests()); ``` -------------------------------- ### Get Current URI After Redirects in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Demonstrates how to obtain the current URI (the last redirected URL) as a Psr\Http\Message\UriInterface object when the 'follow redirects' option is enabled. ```php $request->getCurrentUri(); ``` -------------------------------- ### Initialize EasyRequest with Comprehensive Default Options Source: https://github.com/shtse8/easyrequest/blob/master/README.md Illustrates the full range of default options available when creating an EasyRequest client. This includes settings for protocol, method, headers, body, query parameters, form data, file uploads, cookies, network binding, proxy, authentication, timeouts, redirects, and custom cURL options. ```php $request = \EasyRequest\Client::request('https://google.com', 'POST', array( 'protocol_version' => '1.1', 'method' => 'GET', 'header' => array(), 'body' => '', 'body_as_json' => false, 'query' => array(), 'form_param' => array(), 'multipart' => array(), 'default_header' => true, // add general headers as browser does 'upload' => false, // wanna upload large files ? 'cookie_jar' => null, // file path or CookieJarInterface 'bindto' => null, // bind to an interface (IPV4 or IPV6), same as CURLOPT_INTERFACE 'proxy' => null, 'proxy_type' => null, 'proxy_userpwd' => null, 'auth' => null, 'timeout' => 10, // connect timeout 'nobody' => false, // get header only, you also can use HEAD method 'follow_redirects' => false, // boolean or integer (number of max follow redirect) 'handler' => null, // handler for sending request 'curl' => array(), // use more curl options ? )); ``` -------------------------------- ### Uploading Files with EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section provides a simplified method for uploading files using the EasyRequest client. It demonstrates the `withFormFile` method, which allows specifying the file path, an optional custom filename, and optional headers for the file part, offering an easier alternative to direct multipart configuration. ```php $request ->withFormFile('file1', '/path/to/file1', $optionalFileName = null, $optionalHeaders = array()) ->withFormFile('file2', '/path/to/file2'); ``` -------------------------------- ### Override Request Options with withOption Method in EasyRequest Source: https://github.com/shtse8/easyrequest/blob/master/README.md Shows how to modify existing request options using the `withOption` method. It demonstrates setting options during client creation, and then overriding single or multiple options after the request object has been instantiated. ```php $request = \EasyRequest\Client::request('https://google.com', 'GET', array( 'follow_redirects' => 3, 'query' => 'a=b&c=d' )); // or $request->withOption('follow_redirects', 3); // or $request->withOption(array( 'follow_redirects' => 3, 'query' => 'a=b&c=d' )); ``` -------------------------------- ### Posting Raw Data with EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section shows how to send raw data as the request body using the EasyRequest client. It demonstrates the straightforward `withBody` method for setting the entire request body content directly. ```php $request->withBody('raw data'); ``` -------------------------------- ### Managing Cookies with EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section explains how to use `CookieJar` instances (`FileCookieJar`, `SessionCookieJar`, `CookieJar`) to manage cookies for HTTP requests. It demonstrates adding cookies from strings, responses, and associating a `CookieJar` with a client request, as well as retrieving cookies from the response or for specific domains/paths. ```php $jar = new \EasyRequest\Cookie\CookieJar; // or $jar = new \EasyRequest\Cookie\FileCookieJar($filePath); // or $jar = new \EasyRequest\Cookie\SessionCookieJar; // add cookie from string of multiple cookies $jar->fromString('cookie1=value1; cookie2=value2'); // add cookie with more information $jar->add(Cookie::parse('cookie2=value2; path=/; domain=abc.com')); // add cookie from \Psr\Http\Message\ResponseInterface $jar->fromResponse($response); // read more at \EasyRequest\Cookie\CookieJarInterface $request = \EasyRequest\Client::request('https://google.com', 'GET', array( 'cookie_jar' => $jar ))->send(); ``` ```php var_dump($jar->toArray()); // or var_dump($request->getOption('cookie_jar')->toArray()); var_dump((string) $jar); ``` ```php var_dump($jar->getFor($domain, $path)); ``` -------------------------------- ### Retrieve Request and Response Objects in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Explains how to send a request and retrieve the original RequestInterface, the final ResponseInterface, and the raw response object after the request completes. ```php $request = \EasyRequest\Client::request('http://domain.com', 'POST'); $response = $request->send(); // Returns \Psr\Http\Message\RequestInterface var_dump($request->getRequest()); // Returns \Psr\Http\Message\ResponseInterface // Or null if request is not sent or failure var_dump($request->getResponse()); var_dump($response); ``` -------------------------------- ### Adding Query Strings to EasyRequest PHP Requests Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section demonstrates how to add query parameters to an EasyRequest client. It shows two methods: directly via the 'query' option in an array, or dynamically using the `withQuery` method, supporting both string and array formats for parameters and allowing control over appending or replacing existing queries. ```php $options = array( 'query' => 'a=b&c=d' ); // or $options = array( 'query' => array( 'a' => 'b', 'c' => 'd', 'x' => array( 'y', 'z' ), 'x2' => array( 'y2' => 'z2' ) ) ); $request = \EasyRequest\Client::request('https://google.com', 'GET', $options); ``` ```APIDOC withQuery(name: string|array, value: null|string = null, append: bool = true, recursive: bool = false) name: This value may be: - a query string - array of query string value: Optional string value for the query parameter. append: If true, appends to existing query; if false, clears and adds new. recursive: Boolean indicating recursive merging for array values. Returns: self ``` ```php $request->withQuery(array( 'a' => 'b', 'c' => 'd', 'x' => array( 'y', 'z' ), 'x2' => array( 'y2' => 'z2' ) )); // or $request->withQuery('query=value1&key2=value2'); // or $request->withQuery('query', 'value1'); // if you want to clear all existing query and add new, just use false for $append $request->withQuery('query', 'value1', false); ``` -------------------------------- ### Retrieve All Redirected Responses in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Demonstrates how to obtain a collection of all intermediate response objects when the 'follow_redirects' option is active, with each object conforming to PSR-7. ```php var_dump($request->getResponses()); ``` -------------------------------- ### Set Basic Authentication for EasyRequest PHP Client Source: https://github.com/shtse8/easyrequest/blob/master/README.md Illustrates how to apply basic authentication credentials (username and password) to an outgoing request using the 'auth' option. ```php $request = \EasyRequest\Client::request('http://domain.com', 'POST', array( 'auth' => 'user:pass', )); ``` -------------------------------- ### Adding Form Parameters with EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section explains how to add form parameters to an EasyRequest client, which is similar in concept and usage to adding query strings. It highlights the `withFormParam` method, which provides flexible options for setting form data. ```APIDOC withFormParam(name: string|array, value: null|string = null, append: bool = true, recursive: bool = false) name: This value may be: - query string - array of query string value: Optional string value for the form parameter. append: If true, appends to existing form params; if false, clears and adds new. recursive: Boolean indicating recursive merging for array values. Returns: self ``` -------------------------------- ### Bind EasyRequest PHP Client to Specific Network Interface Source: https://github.com/shtse8/easyrequest/blob/master/README.md Shows how to bind an outgoing request to a specific local IP address using the 'bindto' option, which is equivalent to CURLOPT_INTERFACE. ```php $request = \EasyRequest\Client::request('http://domain.com', 'POST', array( 'bindto' => '123.123.123.123', // same as CURLOPT_INTERFACE option )); ``` -------------------------------- ### Access Response Headers and Body with PSR-7 in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Shows how to interact with the response object using PSR-7 methods to retrieve headers, specific header lines, protocol version, and the response body as a string. ```php $response = $request->getResponse(); // you can use PSR7 here $response->getHeaders(); $response->getHeader('Set-Cookie'); $response->getHeaderLine('Location'); $response->getProtocolVersion(); echo (string) $response->getBody(); ``` -------------------------------- ### Access Error and Debug Information in EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md Describes how to retrieve error messages using `$request->getError()` and general debug information using `$request->getDebug()` when a request fails, aiding in troubleshooting. ```php $error = $request->getError(); $debugInfo = $request->getDebug(); ``` -------------------------------- ### Adding Multipart Data to EasyRequest PHP Requests Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section details how to include multipart data in an EasyRequest. It covers defining parts with names and contents, and optionally specifying filenames and custom headers. It also shows the fluent `withMultipart` method for adding individual parts, including file uploads. ```php $request = \EasyRequest\Client::request('https://google.com', 'GET', array( 'multipart' => array( array( 'name' => 'input1', 'contents' => 'value1' ), array( 'name' => 'input1', 'contents' => 'value1', 'filename' => 'custom file name.txt', 'headers' => array('Custom-header' => 'value'), ) ), )); ``` ```php $request ->withMultipart('field2', 'value2') ->withMultipart('field3', 'value3', 'fieldname3') ->withMultipart('field4', 'value4', 'fieldname4', array('Custom-Header' => 'value')) ->withMultipart('file5', fopen('/path/to/file'), 'filename1') // to upload file ``` -------------------------------- ### Posting JSON Data with EasyRequest PHP Source: https://github.com/shtse8/easyrequest/blob/master/README.md This section explains how to easily send JSON encoded data as the request body. The `withJson` method automatically handles JSON encoding and sets the `Content-Type: application/json` header if not already present, simplifying JSON payload transmission. ```php $request->withJson(array(1,2,3)); // or $request->withJson(json_encode(array(1,2,3))); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.