PHP code example of swnck / pure-request

1. Go to this page and download the library: Download swnck/pure-request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swnck / pure-request example snippets


$request = new PureRequest((new RequestConfiguration())
    ->setReturnTransfer(true) // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
    ->setFollowLocation(true) // Follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set)
    ->setConnectTimeout(10) // The maximum number of seconds to allow cURL functions to execute
);

$request = new PureRequest();

$request->get(HeaderContent::empty(), function (ResponseFrame $response) {
    echo $response->getContent();
    echo $response->getStatusCode();
}, "https://example.com/api/data");

$request->post(HeaderContent::paste(["Content-Type" => ContentType::APPLICATION_JSON, "Connection" => "keep-alive"]), BodyContent::paste([
    "email" => "[email protected]",
    "password" => "your_password"
]), function (ResponseFrame $response) {
    echo $response->getContent();
}, "https://example.com/api/login");