Skip to content

Instantly share code, notes, and snippets.

@sikofitt
Last active July 2, 2021 19:51
Show Gist options
  • Save sikofitt/f78bec648abb1928e4c085548a947f9a to your computer and use it in GitHub Desktop.
Save sikofitt/f78bec648abb1928e4c085548a947f9a to your computer and use it in GitHub Desktop.
Guzzle promise example and Pool example
{
"name": "sikofitt/guzzle-example",
"description": "Guzzle Example",
"type": "project",
"require": {
"php":">=5.6",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"symfony/var-dumper": "^3.2",
"friendsofphp/php-cs-fixer": "^2.1"
},
"config": {
"platform": {
"php":"5.6"
}
},
"license": "GPL-3.0",
"authors": [
{
"name": "sikofitt",
"email": "[email protected]"
}
]
}
<?php
/*
* Guzzle Examples
* Copyleft (Ɔ) 2017 https://blog.rewiv.com sikofitt <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See <http://www.gnu.org/licenses/>.
*
*/
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlFactory;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
require __DIR__.'/vendor/autoload.php';
define('MAX_CURL_HANDLES', 100);
define('MAX_REDIRECTS', 10);
define('REQUEST_TIMEOUT', 30);
define('OUTPUT_DIR', __DIR__.'/images');
define('GUZZLE_CONCURRENCY', 5);
if (false === file_exists(OUTPUT_DIR)) {
mkdir(OUTPUT_DIR);
}
$urls = [
'http://placehold.it/350x153.png',
'http://placehold.it/450x251.png',
'http://placehold.it/659x257.png',
];
if (extension_loaded('curl')) {
$handler = HandlerStack::create(
new CurlMultiHandler([
'handle_factory' => new CurlFactory(MAX_CURL_HANDLES),
'select_timeout' => REQUEST_TIMEOUT,
]));
} else {
$handler = HandlerStack::create();
}
// Create the client and turn off Exception throwing.
$client = new Client([
// No exceptions of 404, 500 etc.
'http_errors' => false,
'handler' => $handler,
// Curl options, any CURLOPT_* option is available
'curl' => [
CURLOPT_BINARYTRANSFER => true,
],
RequestOptions::CONNECT_TIMEOUT => REQUEST_TIMEOUT,
// Allow redirects?
// Set this to RequestOptions::ALLOW_REDIRECTS => false, to turn off.
RequestOptions::ALLOW_REDIRECTS => [
'max' => MAX_REDIRECTS, // allow at most 10 redirects.
'strict' => true, // use "strict" RFC compliant redirects.
'track_redirects' => false,
],
]);
$generator = function (array $urls) {
foreach ($urls as $url) {
$request = new Request('GET', $url);
yield $request;
}
};
$pool = new Pool(
$client,
$generator($urls),
[
'concurrency' => GUZZLE_CONCURRENCY,
'fulfilled' => function (Response $response) {
// Get the last part of the url as the filename.
$image = $response->getBody()->getContents();
// However you want to name the files..
// Just an example.
$imageTypes = sprintf('/(%s)/i', implode('|', ['gif', 'png', 'jpeg', 'jpg', 'tiff']));
preg_match($imageTypes, $image, $matches);
$fileName = true === isset($matches[0]) ?
sprintf('%s.%s', md5(microtime(true)), strtolower($matches[0])) :
sprintf('%s.tmp', md5(microtime(true)));
file_put_contents(OUTPUT_DIR.'/'.$fileName, $image);
},
'rejected' => function (Response $response) {
fwrite(STDOUT, sprintf('Failed getting data, code was %d.', $response->getStatusCode()));
},
]
);
// create the promises
$promise = $pool->promise();
// wait for promises to finish.
$promise->wait(true);
<?php
/*
* Guzzle Examples
* Copyleft (Ɔ) 2017 https://blog.rewiv.com sikofitt <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See <http://www.gnu.org/licenses/>.
*
*/
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlFactory;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
use function GuzzleHttp\Promise\unwrap;
require __DIR__.'/vendor/autoload.php';
define('MAX_CURL_HANDLES', 100);
define('MAX_REDIRECTS', 10);
define('REQUEST_TIMEOUT', 30);
/**
* Go through $urls and create the guzzle promises.
*
* @param array $urls
* Array of urls to try and retrieve
*
* @return array
* An array of Response objects
*/
function createPromises(array $urls)
{
if (extension_loaded('curl')) {
$handler = HandlerStack::create(
new CurlMultiHandler([
'handle_factory' => new CurlFactory(MAX_CURL_HANDLES),
'select_timeout' => REQUEST_TIMEOUT,
]));
} else {
$handler = HandlerStack::create();
}
// Create the client and turn off Exception throwing.
$client = new Client([
// No exceptions of 404, 500 etc.
'http_errors' => false,
'handler' => $handler,
// Curl options, any CURLOPT_* option is available
'curl' => [
CURLOPT_BINARYTRANSFER => true,
],
RequestOptions::CONNECT_TIMEOUT => REQUEST_TIMEOUT,
// Allow redirects?
// Set this to RequestOptions::ALLOW_REDIRECTS => false, to turn off.
RequestOptions::ALLOW_REDIRECTS => [
'max' => MAX_REDIRECTS, // allow at most 10 redirects.
'strict' => true, // use "strict" RFC compliant redirects.
'track_redirects' => false,
],
]);
$promises = [];
foreach ($urls as $key => $url) {
$promises[$key] = $client->getAsync((string) $url);
}
return $promises;
}
$urls = [
'http://placehold.it/350x153.png',
'http://placehold.it/450x251.png',
'http://placehold.it/659x257.png',
];
if (false === file_exists('images')) {
mkdir('images');
}
$promises = createPromises($urls);
$results = unwrap($promises);
/**
* @var int $key
* @var Response $response
*/
foreach ($results as $key => $response) {
$data = [];
// Make sure we have something
if (in_array($response->getStatusCode(), [200, 201], true)) {
// Get the last part of the url as the filename.
$urlArray = explode('/', $urls[$key]);
// Extract the filename
$fileName = end($urlArray);
// Write the contents of the image to a file.
file_put_contents(__DIR__.'/images/'.$fileName, $response->getBody()->getContents());
} else {
fwrite(STDOUT, sprintf('Failed getting data for %s. Code was %d.', $urls[$key], $response->getStatusCode()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment