Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function postToTwitter($data=array()) {
require_once 'OAuth.php';
$consumerKey = 'XXXX';
$consumerSecret = 'XXXX';
$accessToken = 'XXXX';
$accessTokenSecret = 'XXXX';
$consumer = new Twitter_OAuthConsumer($consumerKey, $consumerSecret);
$token = new Twitter_OAuthConsumer($accessToken, $accessTokenSecret);
$signatureMethod = new Twitter_OAuthSignatureMethod_HMAC_SHA1();
$method = 'POST';
$resource = 'https://api.twitter.com/1.1/statuses/update.json';
if (isset($data['media[]'])) {
$resource = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
}
$request = Twitter_OAuthRequest::from_consumer_and_token(
$consumer,
$token,
$method,
$resource,
$data
);
$request -> sign_request(
$signatureMethod,
$consumer,
$token
);
$options = array(
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_URL => $request -> to_url(),
CURLOPT_TIMEOUT => 20,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTPHEADER => array('Expect:'),
CURLOPT_USERAGENT => 'adactio.com',
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, true);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment