Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created May 27, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michabbb/d456ab0966e4baa194bc6053d4e50391 to your computer and use it in GitHub Desktop.
Save michabbb/d456ab0966e4baa194bc6053d4e50391 to your computer and use it in GitHub Desktop.
Upload Images to Ebay in parallel
<?php
/**
the composer.json used here:
{
"require": {
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"kint-php/kint": "^3.2"
}
}
**/
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
require 'vendor/autoload.php';
$client = new Client(['base_uri' => 'https://api.ebay.com/', 'debug' => true, 'exceptions' => false]);
$images[] = file_get_contents('/tmp2/img_data_main.png');
$images[] = file_get_contents('/tmp2/img_data_2.png');
$images[] = file_get_contents('/tmp2/img_data_3.png');
// sequential
/*foreach ($images as $ImageData) {
$bla = $client->post('/ws/api.dll', [
'headers' => [
'X-EBAY-API-APP-NAME' => 'xxxxxxxxx',
'X-EBAY-API-CERT-NAME' => 'xxxxxxxxx',
'X-EBAY-API-DEV-NAME' => 'xxxxxxxxx',
'X-EBAY-API-CALL-NAME' => 'UploadSiteHostedPictures',
'X-EBAY-API-COMPATIBILITY-LEVEL' => '1113',
'X-EBAY-API-SITEID' => '77',
],
'multipart' => [
[
'name' => 'xml payload',
'contents' => '<?xml version="1.0" encoding="utf-8"?>
<UploadSiteHostedPicturesRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<ebl:eBayAuthToken xmlns:ebl="urn:ebay:apis:eBLBaseComponents">xxxxxxxx</ebl:eBayAuthToken>
</RequesterCredentials>
<PictureName>something</PictureName>
<PictureSet>Standard</PictureSet>
<ExtensionInDays>1</ExtensionInDays>
</UploadSiteHostedPicturesRequest>',
],
[
'name' => 'image data',
'filename' => 'something.png',
'contents' => $ImageData
]
]
]);
d($bla->getBody()->getContents());
}
exit;*/
// parallel
$requests = function ($images) use ($client) {
foreach ($images as $imageData) {
yield function () use ($client, $imageData) {
return $client->postAsync('/ws/api.dll', [
'headers' => [
'X-EBAY-API-APP-NAME' => 'xxxxxxxx',
'X-EBAY-API-CERT-NAME' => 'xxxxxxxx',
'X-EBAY-API-DEV-NAME' => 'xxxxxxxx',
'X-EBAY-API-CALL-NAME' => 'UploadSiteHostedPictures',
'X-EBAY-API-COMPATIBILITY-LEVEL' => '1113',
'X-EBAY-API-SITEID' => '77',
],
'multipart' => [
[
'name' => 'xml payload',
'contents' => '<?xml version="1.0" encoding="utf-8"?>
<UploadSiteHostedPicturesRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<ebl:eBayAuthToken xmlns:ebl="urn:ebay:apis:eBLBaseComponents">xxxxxxxx</ebl:eBayAuthToken>
</RequesterCredentials>
<PictureName>something</PictureName>
<PictureSet>Standard</PictureSet>
<ExtensionInDays>1</ExtensionInDays>
</UploadSiteHostedPicturesRequest>',
],
[
'name' => 'image data',
'filename' => 'something.png',
'contents' => $imageData,
]
]
]);
};
}
};
$pool = new Pool($client, $requests($images), [
'concurrency' => 5,
'fulfilled' => function (GuzzleHttp\Psr7\Response $response, $index) {
d($index, $response->getHeaders(),$response->getBody()->getContents());
},
'rejected' => function ($reason, $index) {
d($index, $reason);
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete
$promise->wait();
d($pool);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment