PHP code example of sylouuu / php-curl

1. Go to this page and download the library: Download sylouuu/php-curl 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/ */

    

sylouuu / php-curl example snippets




// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Template
$request = new Curl\<METHOD_NAME>( string $url [, array $options ] );

[
    'data' => [             // Data to send, available for `Post`, `Put` and `Patch`
        'foo' => 'bar'
    ],
    'headers' => [          // Additional headers (optional)
        'Authorization: foobar'
    ],
    'ssl' => '/cacert.pem', // Use it for SSL (optional)
    'is_payload' => true,   // `true` for sending a payload (JSON-encoded data, optional)
    'autoclose' => true     // Is the request must be automatically closed (optional)
]

// Send a request
$request->send();

// HTTP status code
$request->getStatus();

// HTTP header
$request->getHeader();

// HTTP body response
$request->getResponse();

// Used cURL options
$request->getCurlOptions();

// Set a cURL option
$request->setCurlOption(CURLOPT_SOMETHING, $value);

// Manually close the handle (necessary when `autoclose => false` is used)
$request->close();

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Standard GET request
$request = new Curl\Get('http://domain.com');

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// JSON-encoded POST request
$request = new Curl\Post($this->endpoint, [
    'data' => [
        'name' => 'foo',
        'email' => '[email protected]'
    ],
    // With 'is_payload' => true
    // You don't have to json_encode() your array of data
    // Moreover, the appropriate headers will be set for you
    'is_payload' => true
]);

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Set `autoclose` option to `false`
$request = new Curl\Get('http://domain.com', [
    'autoclose' => false
]);

// Send this request
$request->send();

// Now you can retrieve a cURL info as the handle is still open
$request->getCurlInfo(CURLINFO_SOMETHING);

echo $request->getResponse();

// Manually close the handle
$request->close();
js
{
    ""sylouuu/php-curl": "0.8.*"
    }
}