1 <?php |
1 <?php |
2 |
2 |
3 /** |
|
4 * include some common code (like we did in the 90s) |
|
5 * People still do this? ;) |
|
6 */ |
|
7 include_once 'common.php'; |
3 include_once 'common.php'; |
|
4 use Abraham\TwitterOAuth\TwitterOAuth; |
8 |
5 |
9 /** |
6 /** |
10 * Check for a POSTed status message to send to Twitter |
7 * Check for a POSTed status message to send to Twitter |
11 */ |
8 */ |
12 if (!empty($_POST) && isset($_POST['status']) |
9 if (!empty($_POST) && isset($_POST['status']) |
13 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) { |
10 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) { |
14 /** |
11 |
15 * Easiest way to use OAuth now that we have an Access Token is to use |
|
16 * a preconfigured instance of Zend_Http_Client which automatically |
|
17 * signs and encodes all our requests without additional work |
|
18 */ |
|
19 $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']); |
12 $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']); |
20 $client = $token->getHttpClient($configuration); |
13 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']); |
21 $client->setUri('https://api.twitter.com/1.1/statuses/update.json'); |
|
22 $client->setMethod(Zend_Http_Client::POST); |
|
23 $client->setParameterPost('status', $_POST['status']); |
|
24 $response = $client->request(); |
|
25 |
14 |
26 /** |
15 $data = $connection->post("statuses/update", ["status" => $_POST['status']]); |
27 * Check if the json response refers to our tweet details (assume it |
16 |
28 * means it was successfully posted). API gurus can correct me. |
17 // $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']); |
29 */ |
18 // $client = $token->getHttpClient($configuration); |
30 $data = json_decode($response->getBody()); |
19 // $client->setUri('https://api.twitter.com/1.1/statuses/update.json'); |
31 $result = $response->getBody(); |
20 // $client->setMethod(Zend_Http_Client::POST); |
|
21 // $client->setParameterPost('status', $_POST['status']); |
|
22 // $response = $client->request(); |
|
23 |
|
24 // /** |
|
25 // * Check if the json response refers to our tweet details (assume it |
|
26 // * means it was successfully posted). API gurus can correct me. |
|
27 // */ |
|
28 // $data = json_decode($response->getBody()); |
|
29 // $result = $response->getBody(); |
32 if (isset($data->text)) { |
30 if (isset($data->text)) { |
33 $result = 'true'; |
31 $result = 'true'; |
|
32 } else { |
|
33 $result = json_encode($data); |
34 } |
34 } |
35 /** |
35 /** |
36 * Tweet sent (hopefully), redirect back home... |
36 * Tweet sent (hopefully), redirect back home... |
37 */ |
37 */ |
38 //header('Location: ' . URL_ROOT . '?result=' . $result); |
38 //header('Location: ' . URL_ROOT . '?result=' . $result); |