Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Created May 30, 2016 08:19
Show Gist options
  • Save notgiorgi/578509e16cd6ceccaa6058adeeda1ac3 to your computer and use it in GitHub Desktop.
Save notgiorgi/578509e16cd6ceccaa6058adeeda1ac3 to your computer and use it in GitHub Desktop.
<?php
use GuzzleHttp\Client;
/**
* Makes request to save base64 image on imgur
* @param String $image
* @return String $url
*/
function saveToImgur($image)
{
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://api.imgur.com/3/image',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$auth = 'Client-ID ' . env('IMGUR_CLIENT_ID');
$headers = ['Authorization' => $auth, ];
$body = [ 'image' => 'https://www.google.ge/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' ];
try {
$res = $client->post('', [
'headers' => $headers,
'json' => ($body)
]);
} catch (\Exception $e) {
return $e->getMessage();
}
if($res->getStatusCode() != 200)
return $res->getStatusCode();
$res = $res->getBody()->getContents();
$image = json_decode($res);
return $image->data->link;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment