1. Go to this page and download the library: Download jublonet/codebird-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jublonet / codebird-php example snippets
\Codebird\Codebird::setConsumerKey('YOURKEY', 'YOURSECRET'); // static, see README
$cb = \Codebird\Codebird::getInstance();
$cb->setToken('YOURTOKEN', 'YOURTOKENSECRET');
session_start();
if (! isset($_SESSION['oauth_token'])) {
// get the request token
$reply = $cb->oauth_requestToken([
'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
]);
// store the token
$cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
$_SESSION['oauth_verify'] = true;
// redirect to auth website
$auth_url = $cb->oauth_authorize();
header('Location: ' . $auth_url);
die();
} elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
// verify the token
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
unset($_SESSION['oauth_verify']);
// get the access token
$reply = $cb->oauth_accessToken([
'oauth_verifier' => $_GET['oauth_verifier']
]);
// store the token (which is different from the request token!)
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
// send to same URL, without oauth GET parameters
header('Location: ' . basename(__FILE__));
die();
}
// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// these files to upload. You can also just upload 1 image!
$media_files = [
'bird1.jpg', 'bird2.jpg', 'bird3.jpg'
];
// will hold the uploaded IDs
$media_ids = [];
foreach ($media_files as $file) {
// upload all media files
$reply = $cb->media_upload([
'media' => $file
]);
// and collect their IDs
$media_ids[] = $reply->media_id_string;
}
// convert media ids to string list
$media_ids = implode(',', $media_ids);
// send Tweet with these medias
$reply = $cb->statuses_update([
'status' => 'These are some of my relatives.',
'media_ids' => $media_ids
]);
print_r($reply);
$file = 'demo-video.mp4';
$size_bytes = filesize($file);
$fp = fopen($file, 'r');
// INIT the upload
$reply = $cb->media_upload([
'command' => 'INIT',
'media_type' => 'video/mp4',
'total_bytes' => $size_bytes
]);
$media_id = $reply->media_id_string;
// APPEND data to the upload
$segment_id = 0;
while (! feof($fp)) {
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
$reply = $cb->media_upload([
'command' => 'APPEND',
'media_id' => $media_id,
'segment_index' => $segment_id,
'media' => $chunk
]);
$segment_id++;
}
fclose($fp);
// FINALIZE the upload
$reply = $cb->media_upload([
'command' => 'FINALIZE',
'media_id' => $media_id
]);
var_dump($reply);
if ($reply->httpstatus < 200 || $reply->httpstatus > 299) {
die();
}
// if you have a field `processing_info` in the reply,
// use the STATUS command to check if the video has finished processing.
// Now use the media_id in a Tweet
$reply = $cb->statuses_update([
'status' => 'Twitter now accepts video uploads.',
'media_ids' => $media_id
]);
// First, create a callback function:
function some_callback($message)
{
// gets called for every new streamed message
// gets called with $message = NULL once per second
if ($message !== null) {
print_r($message);
flush();
}
// return false to continue streaming
// return true to close the stream
// close streaming after 1 minute for this simple sample
// don't rely on globals in your code!
if (time() - $GLOBALS['time_start'] >= 60) {
return true;
}
return false;
}
// set the streaming callback in Codebird
$cb->setStreamingCallback('some_callback');
// any callable is accepted:
// $cb->setStreamingCallback(['MyClass', 'some_callback']);
// for canceling, see callback function body
// not considered good practice in real world!
$GLOBALS['time_start'] = time();
// Second, start consuming the stream:
$reply = $cb->statuses_filter();
// See the *Mapping API methods to Codebird function calls* section for method names.
// $reply = $cb->statuses_filter('track=Windows');
$original_tweet = [
'id_str' => '684483801687392256',
'user' => [
'screen_name' => 'LarryMcTweet'
]
];
$original_tweet = (object) $original_tweet; // sample, get real Tweet from API
$id = $original_tweet->id_str; // use the `id_str` field because of long numbers
$screen_name = $original_tweet->user->screen_name;
// looks like this: https://twitter.com/LarryMcTweet/status/684483801687392256
$url = "https://twitter.com/$screen_name/status/$id";
$text = 'I’d like to quote a Tweet.'; // maximum length = 140 minus 24 (link length) minus 1 space
$reply = $cb->statuses_update([
'status' => "$text $url"
]);