PHP code example of half2me / curlx

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

    

half2me / curlx example snippets


use CurlX\Agent;

$agent = new Agent(10);

$request = $agent->newRequest('http://myurl.com'); // URL can optionally be set here
$request->url = 'http://myurl.com'; // or here
$request->timeout = 5000; // We can set different timeout values (in msec) for each request
$request->post_data = ['Agents' => 'AreCool']; // We can add post fields as arrays
$request->post_data = ['MoreAgents' => 'AreCooler']; // This will be appended to the post values already set
$request->headers = ['Content-type' => 'agent/xml', 'Authorization' => 'ninja-stuff']; // Headers can easily be set
$request->headers = ['Agent-type: Ninja']; // These will be appended to the header list
$request->options = ['CURLOPT_SOME_OPTION' => 'your-value']; // Advanced options can be set for cURL
$request->options = ['CURLOPT_SOME_OTHER_OPTION' => 'your-other-value']; // Chain these up, or add many in one array

// The Agent already has this request in his queue, so we don't need to do anything after modifying requests options.

$agent->post_data = ['AllAgents' => 'AreCool'];
$request = $agent->newRequest();

echo $request->post_data['AllAgents']; // this will output 'AreCool'

// of course we can always overwrite this:
$request->post_data = ['AllAgents' => 'AreSuperDuperCool']; // This will overwrite that post value

$request1 = $agent->newRequest();
$request2 = $agent->newRequest();

$agent->execute();

$request1->addListener('myCallbackFunction'); // For a single request
$agent->addListener('myCallbackFunction'); // For all requests to use the same callback
// Note, this will only apply to requests made after the addListener() was called.

// You can use anonymous functions for callbacks like this:
$request->addListener(function(CurlX\RequestInterface $request) {
    // Each listener (or callback function) will upon request completion receieve
    // in the function parameter, the completed request object
    
    echo $request->response; // Response is stored here
    echo $request->http_code; // Get the http code of the reply
});