Display Twitter tweets on website



Using updateCase you can display your latest tweets on your website. 

# 2 > Details

Fill in the details for your APP
-> Name: The name that will display in the settings (eg your company name)
-> Description: A sentance what your apps purpose is to do
-> Website: The website which will access (Use your live website www.yourdomain.com )
-> Callback url: You can leave this blank as this is only for more advanced programming
-> Check box that you have read the developer agreement
-> click 'Create your twitter application'


# 3 > Keys

Click on 'Keys and Access Tokens'

Copy and paste the following to your PHP script in the next script


# 4 > Element

Create an element (app/View/Elements/a/twitter.ctp)
-> And paste in the following code
-> Changle all the UPPERCASE information per the previous step

 

<?php // auth parameters
$api_key = urlencode('CONSUMER_API_KEY_HERE'); // Consumer Key (API Key)
$api_secret = urlencode('CONSUMER_SECRET_API_SECRET'); // Consumer Secret (API Secret)
$auth_url = 'https://api.twitter.com/oauth2/token';
// what we want?
$data_username = 'USERNAME'; // username
$data_count = 1; // number of tweets
$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended';
// get api access token
$api_credentials = base64_encode($api_key.':'.$api_secret);
$auth_headers = 'Authorization: Basic '.$api_credentials."\r\n".
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n";
$auth_context = stream_context_create(
array(
'http' => array(
'header' => $auth_headers,
'method' => 'POST',
'content'=> http_build_query(array('grant_type' => 'client_credentials', )),
)
)
);
$auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
$auth_token = $auth_response['access_token'];
// get tweets
$data_context = stream_context_create( array( 'http' => array( 'header' => 'Authorization: Bearer '.$auth_token."\r\n", ) ) );
$data = json_decode(file_get_contents($data_url.'&count='.$data_count.'&screen_name='.urlencode($data_username), 0, $data_context), true); ?>

<?= $data[0]['full_text']; ?>

<?php if (0): //do not use this right now ?>
<?php foreach ($data as $each): //Or if you want to loop through tweets ?>
<?= $each['full_text']; ?>
<?php endforeach; ?>
<?php endif; //hide it ?>

# 5 > Display

Now that an element has been created we can view this in a view page

<?= $this->Element('a/twitter'); ?>

Other Instructions

Below are many other instructions that show you how to use your UpdateCase application