-
Notifications
You must be signed in to change notification settings - Fork 32
/
tweet-with-media.php
33 lines (27 loc) · 1.51 KB
/
tweet-with-media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
session_start();
require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', ''); // add your app consumer key between single quotes
define('CONSUMER_SECRET', ''); // add your app consumer secret key between single quotes
define('OAUTH_CALLBACK', 'https://sohaibilyas.com/twapp/callback.php'); // your app callback URL
if (!isset($_SESSION['access_token'])) {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
echo $url;
} else {
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
// getting basic user info
$user = $connection->get("account/verify_credentials");
// printing username on screen
echo "Welcome " . $user->screen_name;
// uploading media (image) and getting media_id
$tweetWM = $connection->upload('media/upload', ['media' => 'https://pbs.twimg.com/profile_images/695720184464740353/lnOGP0Z8_400x400.jpg']);
// tweeting with uploaded media (image) using media_id
$tweet = $connection->post('statuses/update', ['media_ids' => $tweetWM->media_id, 'status' => 'tweeting with image file']);
print_r($tweet);
}