2017 © Pedro Peláez
 

library curlx

Easy to use wrapper for cUrl Multi

image

half2me/curlx

Easy to use wrapper for cUrl Multi

  • Thursday, March 3, 2016
  • by halftome
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2,539 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 36 Forks
  • 2 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

CurlX

Software License Build Status Scrutinizer Code Quality codecov.io Total Downloads Latest Stable Version Latest Unstable Version
Curl X is a fork of RollingCurlX. At fist I only created this fork to make it installable via composer for a project I was working on. Now it is a modern, easy-to-use, awesome wrapper for cUrl Multi Handler. With Agents and Requests, take a look at how easy everything has become., (*1)

License

MIT, (*2)

Requirements

PHP 5.6+, (*3)

Installing

Installing is easy with composer. Just do composer require half2me/curlx:^1.0, (*4)

How to Use

First we initialize an agent with the maximum number of concurrent requests we want open at a time. All requests after this will be queued until one completes., (*5)

use CurlX\Agent;

$agent = new Agent(10);

Next we create/add a request to the queue, (*6)

$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.

Most of the values that can be set on individual Requests can also be set for an agent When an agent has these values set, any requests created by that agent, will have these parameters set; If we have many requests using similar headers, urls, or timeout values, we can set these once in the Agent, and use them in all of the requests. For example:, (*7)

$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

Once we have our agent loaded up with requests, (*8)

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

We can start executing them with:, (*9)

$agent->execute();

As a request finishes, it will fire an event which we need to hook onto, before we start the agent. For this we need to register one or more callback functions with either the agent (to use the same for all requests) or we can register a separate callback function for each request., (*10)

$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
});

Issues

If you find any issues please let me know. Submit an issue or PR on github, (*11)

Enjoy., (*12)

The Versions

03/03 2016

dev-master

9999999-dev https://github.com/half2me/curlx

Easy to use wrapper for cUrl Multi

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

curl async thread multi

12/02 2016

1.0.0

1.0.0.0 https://github.com/half2me/curlx

Easy to use wrapper for cUrl Multi

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

curl async thread multi