Giter Site home page Giter Site logo

curlx's Introduction

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.

####License MIT

Requirements

PHP 5.6+

Installing

Installing is easy with composer. Just do composer require half2me/curlx:^1.0

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

use CurlX\Agent;

$agent = new Agent(10);

Next we create/add a request to the queue

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

$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

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

We can start executing them with:

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

$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

Enjoy.

curlx's People

Contributors

daipratt avatar marcushat avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

curlx's Issues

Add an example/explanation for the agent's default options

It is not clear whether something like this would work or not -

    $agent->options = ['CURLOPT_SOME_OPTION' => 'your-value'];

And would cause the new requests to use this option or not.

Please consider adding an example/explanation to make it clearer.

Thanks

Userdata

Is there a way to include userdata in the requests section? I'm using rollingcurlx in a project in which I need to reference a database object.

Custom Variables

How do you pass in custom variables , in the original
function callback_functn($response, $url, $request_info, $user_data, $time) {
$time; //how long the request took in milliseconds (float)
$request_info; //returned by curl_getinfo($ch)
}

It was user_data, what is it now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.