• Stars
    star
    374
  • Rank 114,346 (Top 3 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created almost 15 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A high performance PHP library for using multi curl for parallel http calls.

PHP-multi-curl

High performance PHP curl wrapper to make parallel HTTP calls

Build Status Scrutinizer Code Quality

Contents

Installation

You can use composer to install this library from the command line.

composer require jmathai/php-multi-curl:dev-master -v

Usage

Basic usage can be done using the addUrl($url/*, $options*/) method. This calls GET $url by passing in $options as the parameters.

<?php
  // Include Composer's autoload file if not already included.
  require '../vendor/autoload.php';

  // Instantiate the MultiCurl class.
  $mc = JMathai\PhpMultiCurl\MultiCurl::getInstance();

  // Make a call to a URL.
  $call1 = $mc->addUrl('http://slowapi.herokuapp.com/delay/2.0');
  // Make another call to a URL.
  $call2 = $mc->addUrl('http://slowapi.herokuapp.com/delay/1.0');

  // Access the response for $call2.
  // This blocks until $call2 is complete without waiting for $call1
  echo "Call 2: {$call2->response}\n";

  // Access the response for $call1.
  echo "Call 1: {$call1->response}\n";

  // Output a call sequence diagram to see how the parallel calls performed.
  echo $mc->getSequence()->renderAscii();

This is what the output of that code will look like.

Call 2: consequatur id est
Call 1: in maiores et
(http://slowapi.herokuapp.com/delay/2.0 ::  code=200, start=1447701285.5536, end=1447701287.9512, total=2.397534)
[====================================================================================================]
(http://slowapi.herokuapp.com/delay/1.0 ::  code=200, start=1447701285.5539, end=1447701287.0871, total=1.532997)
[================================================================                                    ]

Advanced Usage

You'll most likely want to configure your cURL calls for your specific purpose. This includes setting the call's HTTP method, parameters, headers and more. You can use the addCurl($ch) method and configuring your curl handle using any of PHP's curl_* functions.

<?php
  $mc = JMathai\PhpMultiCurl\MultiCurl::getInstance();

  // Set up your cURL handle(s).
  $ch = curl_init('http://www.google.com');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  
  // Add your cURL calls and begin non-blocking execution.
  $call = $mc->addCurl($ch);
  
  // Access response(s) from your cURL calls.
  $code = $call->code;

You can look at the tests/example.php file for working code and execute it from the command line.

Documentation

Methods

addUrl

addUrl((string) $url/*, (array) $options*/)

Makes a GET call to $url by passing the key/value array $options as parameters. This method automatically sets CURLOPT_RETURNTRANSFER to 1 internally.

$call = $mc->addUrl('https://www.google.com', array('q' => 'github'));
echo $call->response;

addCurl

addCurl((curl handle) $ch)

Takes a curl handle $ch and executes it. This method, unlike addUrl, will not add anything to the cURL handle. You'll most likely want to set CURLOPT_RETURNTRANSFER yourself before passing the handle into addCurl.

$ch = curl_init('http://www.mocky.io/v2/5185415ba171ea3a00704eed');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

$call = $mc->addCurl($ch);
echo $call->response;

Accessing Response

The curl calls begin executing the moment you call addUrl or addCurl. Execution control is returned to your code immediately and blocking for the response does not occur until you access the response or code variables. The library only blocks for the call you're trying to access the response to and will allow longer running calls to continue to execute while returning control back to your code.

response

The response variable returns the string text of the response.

echo $call->response;

code

The code variable returns the integer HTTP response code of the request.

echo $call->code;

headers

The headers variable returns an array of HTTP headers from the response.

var_dump($call->headers);

renderAscii

Return a string that prints out details of call latency and degree of being called in parallel. This method can be called indirectly through the multi-curl instance you're using.

echo $mc->getSequence()->renderAscii();

Authors

  • jmathai

Contributors

  • Lewis Cowles (LewisCowles1986) - Usability for adding url's without needing to worry about CURL, but provisioning also for specifying additional parameters
  • Sam Thomson (samthomson) - Packaged it up

More Repositories

1

elodie

An EXIF-based photo assistant, organizer and workflow automation tool.
Python
1,227
star
2

epiphany

A micro PHP framework that's fast, easy, clean and RESTful. The framework does not do a lot of magic under the hood. It is, by design, very simple and very powerful.
PHP
687
star
3

twitter-async

Twitter-async is a high performance wrapper for Twitter's OAuth API which provides parallel/asynchronous calls. Follow @apiclient for commit updates.
HTML
533
star
4

foursquare-async

A port of Twitter-async for Foursquare. This is a high performance wrapper for Foursquare's OAuth 2 API which provides parallel/asynchronous calls.
PHP
164
star
5

s3-bucket-stream-zip-php

PHP library to efficiently stream contents from an AWS S3 bucket or folder as a zip file.
PHP
54
star
6

amnesia

Amnesia for Twitter automatically deletes tweets after a specified number of days.
Python
24
star
7

synology-photos-reindexer

Simple script to call Synology Photo's reindex API that can be scheduled to run periodically.
Python
20
star
8

all-hands-on-deck

Python
18
star
9

playground

Ruby
6
star
10

jaisenmathai

jaisenmathai.com
PHP
6
star
11

photos

personal photo gallery
PHP
5
star
12

meltsmyheart

JavaScript
3
star
13

trovebox-sync

PHP
2
star
14

area-code-500px

A group chatting Joule with commands to join and leave powered by Twilio
JavaScript
2
star
15

joule-node-dynamic-dns

A Dynamic DNS system using Joule and AWS Lambda.
JavaScript
2
star
16

configs

1
star
17

csmforchrist

PHP
1
star
18

jmathai.github.com

JavaScript
1
star
19

csm

PHP
1
star
20

csm-emailer

A boilerplate for creating nodejs Joules
JavaScript
1
star
21

openphoto-transfer

Python
1
star