Skip to content

Instantly share code, notes, and snippets.

@igorw
Created October 11, 2012 23:46
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save igorw/3876447 to your computer and use it in GitHub Desktop.
Save igorw/3876447 to your computer and use it in GitHub Desktop.
React Http Client
<?php
require __DIR__.'/vendor/autoload.php';
use React\Curry\Util as Curry;
$projects = array(
'react-php/react',
'cboden/Ratchet',
'nrk/predis-async',
'bergie/dnode-php',
'umpirsky/wisdom',
'igorw/evenement',
);
$fullRequest = function ($url, $callback) {
$callback(file_get_contents($url));
};
$parseResponse = function ($project, $body) {
$decoded = json_decode($body, true);
$latest = $decoded[0]['commit'];
$author = $latest['author']['name'];
$date = date('F j, Y', strtotime($latest['author']['date']));
$message = preg_replace('#\n.*$#s', '', $latest['message']);
printf("\n");
printf("Latest commit on %s was done by %s on %s\n", $project, $author, $date);
printf("%s\n", $message);
};
printf("I'm telling you about %s projects.\n", count($projects));
foreach ($projects as $project) {
$url = "https://api.github.com/repos/$project/commits";
$fullRequest($url, Curry::bind($parseResponse, $project));
}
<?php
require __DIR__.'/vendor/autoload.php';
use React\Curry\Util as Curry;
$loop = React\EventLoop\Factory::create();
$client = new React\HttpClient\Client($loop);
$projects = array(
'react-php/react',
'cboden/Ratchet',
'nrk/predis-async',
'bergie/dnode-php',
'umpirsky/wisdom',
'igorw/evenement',
);
$fullRequest = function ($url, $callback) use ($client) {
$request = $client->request('GET', $url);
$request->on('response', function ($response) use ($callback) {
$buffer = '';
$response->on('data', function ($data) use (&$buffer) {
$buffer .= $data;
});
$response->on('end', function () use (&$buffer, $callback) {
$callback($buffer);
});
});
$request->end();
};
$parseResponse = function ($project, $body) {
$decoded = json_decode($body, true);
$latest = $decoded[0]['commit'];
$author = $latest['author']['name'];
$date = date('F j, Y', strtotime($latest['author']['date']));
$message = preg_replace('#\n.*$#s', '', $latest['message']);
printf("\n");
printf("Latest commit on %s was done by %s on %s\n", $project, $author, $date);
printf("%s\n", $message);
};
printf("I'm telling you about %s projects.\n", count($projects));
foreach ($projects as $project) {
$url = "https://api.github.com/repos/$project/commits";
$fullRequest($url, Curry::bind($parseResponse, $project));
}
$loop->run();
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/arnaud-lb/react"
}
],
"minimum-stability": "dev",
"require": {
"react/react": "dev-http-client",
"react/curry": "dev-master"
}
}
@hpatoio
Copy link

hpatoio commented Mar 7, 2014

Ciao Igor. Is there a way to get all the responses in an hash, instead of printing them ? Something like:

Array
(
    [prj1] => {json from the API}
    [prj2] => {json from the API}
    [prj3] => {json from the API}
)

@kieuduygn
Copy link

Hello I got this error, I'm not familiar with React but I need this script for my project, plz help

Catchable fatal error: Argument 2 passed to React\HttpClient\Client::__construct() must be an instance of React\HttpClient\ConnectionManagerInterface, none given, called in C:\wamp\www\httpclient\index.php on line 5 and defined in C:\wamp\www\httpclient\vendor\react\react\src\React\HttpClient\Client.php on line 19

@valoricDe
Copy link

valoricDe commented Mar 27, 2017

The video on https://vimeo.com/channels/407317/51261181 is really hilarious. It's astonishing that parallel requests are quicker then requests in serial. o_0

@DiDebru
Copy link

DiDebru commented Apr 4, 2017

`Hello I got this error, I'm not familiar with React but I need this script for my project, plz help

Catchable fatal error: Argument 2 passed to React\HttpClient\Client::__construct() must be an instance of React\HttpClient\ConnectionManagerInterface, none given, called in C:\wamp\www\httpclient\index.php on line 5 and defined in C:\wamp\www\httpclient\vendor\react\react\src\React\HttpClient\Client.php on line 19`

Got the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment