Skip to content

Instantly share code, notes, and snippets.

@gdoteof
Last active August 29, 2015 14:12
Show Gist options
  • Save gdoteof/79648375999a7fdad28a to your computer and use it in GitHub Desktop.
Save gdoteof/79648375999a7fdad28a to your computer and use it in GitHub Desktop.
tweet.php
<?php
header('Access-Control-Allow-Origin: *');
require("twitter-php/src/twitter.class.php");
#tokens for twitter api
$consumerKey = '123-xxx';
$consumerSecret = 'yyyy';
$accessToken = '123-abc';
$accessTokenSecret = '123';
# client id for imgur api
$client_id = "";
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$image_dataurl = $_POST['image_data'];
// Grab the MIME type and the data with a regex for convenience
if (!preg_match('/data:([^;]*);base64,(.*)/', $image_dataurl, $matches)) {
var_dump($_POST);
die("error ");
}
// Decode the data
$image = base64_decode($matches[2]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$reply = curl_exec($ch);
curl_close($ch);
$reply = json_decode($reply);
$imgur_id = $reply->data->id;
$imgur_link_url = 'imgur.com/'.$imgur_id;
var_dump($reply->data);
var_dump($reply);
$twitter->send($_POST['tweet'] . ' ' . $imgur_link_url);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment