twitter status update thru api with oauth authentication in php - php

I was googling around for some time and didn't googled anything...
How to, using php, update twitter status thru twitter API with OAuth?
Thanks!

There are some php classes already written for Twitter API. Just pick one and use it.
Twitter developer site lists a bunch of classes right on their site:
http://dev.twitter.com/pages/libraries#php
Or you can use pecl oauth library instead.
I personally prefer using pecl lib, here is a link to useful article:
http://toys.lerdorf.com/archives/50-Using-pecloauth-to-post-to-Twitter.html

Here is php code for twitter status update:
$body = array(
'status'=>'YOURMESSAGE'
);
$url = "https://api.twitter.com/1.1/statuses/update.json";
$oauth_access_token = "YOURVALUE";
$oauth_access_token_secret = "YOURVALUE";
$consumer_key = "YOURVALUE";
$consumer_secret = "YOURVALUE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$base_info = buildBaseString($url, 'POST', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$result_data = json_decode($json);
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}

Related

Unable to get Extended Tweet using PHP

I have an already running Twitter App which I am using to show timeline on my website using PHP CURL. Now I want to use tweet_mode=extended to get 280 characters but it says authentication problem.
Following is my code:
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "XXXXX";
$oauth_access_token_secret = "XXXXX";
$consumer_key = "XXXXX";
$consumer_secret = "XXXXX";
$oauth = array(
'screen_name' => 'MAhmadZ',
'count' => 4,
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
//build the base string
$temp = array();
ksort($oauth);
foreach($oauth as $key => $value):
$temp[] = "$key=" . rawurlencode($value);
endforeach;
$base_info = 'GET&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $temp));
unset( $temp );
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, TRUE));
$oauth['oauth_signature'] = $oauth_signature;
//build authorization header for making request
$temp = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value):
$values[] = "$key=\"" . rawurlencode($value) . "\"";
endforeach;
$temp .= implode(', ', $values);
$header = array($temp, 'Expect:');
unset( $temp );
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => FALSE,
CURLOPT_URL => $url . '?screen_name=MAhmadZ&count=4',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
Use 'tweet_mode=extended' to get extended/full tweets
Go here for more details :https://www.finalwebsites.com/twitter-search-api/
It is problem with the CURL i guess. Use this library to avoid the error:
https://github.com/J7mbo/twitter-api-php

How to reply to the tweets which have mentioned me in their tweets

I'm trying to reply to tweets programmatically, those which have mentioned me in their tweets. So I collected the tweets which have mentioned me from the mention_timeline Api
mentioned_me_tweets.php
<?php
ini_set('displays_error',1);
error_reporting(E_ALL);
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$url = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";
$oauth_access_token = "xxxx";
$oauth_access_token_secret = "yyyy";
$consumer_key = "zzzzzz";
$consumer_secret = "ccccc";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Make requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
//print it out
echo "<pre>";
print_r($twitter_data);
?>
From the above program, I get as output, the collection of tweets which have mentioned me. Now I can reply to their tweets via an API.
How to do this? give me some ideas about it. If you want any information, ask me in comments. I will reply to you soon.
Thanks advance .

Can't get Twitter trends using PHP

So I'm trying to make a fairly simple php script that fetches the latest worldwide trends from Twitter. Most of the code comes from another StackOverflow post, with some minor modifications. Unfortunately, regardless what I try, all I get is:
{"errors":[{"message":"Could not authenticate you","code":32}]}
Yet this same code works just fine to get timelines, get tweets, post tweets, etc... But when I try trends, for some reason it fails.
$twitterurl = "https://api.twitter.com/1.1/trends/place.json?id=1";
$oauth_access_token = "XXXXXXXXXXXXXXXXXXX";
$oauth_access_token_secret = "XXXXXXXXXXXXXXXXXX";
$consumer_key = "XXXXXXXXXXXX";
$consumer_secret = "XXXXXXXXXXXX";
$oauth = array( 'oauth_consumer_key' => $consumer_key, 'oauth_nonce' => time(), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $oauth_access_token, 'oauth_timestamp' => time(), 'oauth_version' => '1.0');
function buildBaseString($baseURI, $method, $params)
{
$r = array();
ksort($params);
foreach($params as $key=>$value)
{
$r[] = "$key=" . rawurlencode($value);
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth)
{
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
{
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$r .= implode(', ', $values);
return $r;
}
$base_info = buildBaseString($twitterurl, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $twitterurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$trends = json_decode($json);
print_r($json);
You can try to modify your php script in these lines:
$twitterurl = "https://api.twitter.com/1.1/trends/place.json"; // DELETE ?id=1
and
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'id' => '1', // ADD THIS ROW
'oauth_nonce' => time(),
and
CURLOPT_URL => $twitterurl . '?id=1', // ADD . '?id=1'
I hope this helps.

Twitter API PHP Get latest tweets not authenticating

I'm using this peice of code to try and get all my latest tweets for printing on my site but it's returning an error about being not authenticated. I've created an app in the Dev section and get my consumer and OAuth keys, and added them in the right place in the code.
<?php
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk";
$oauth_access_token = "SECURITY";
$oauth_access_token_secret = "SECURITY";
$consumer_key = "SECURITY";
$consumer_secret = "SECURITY";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json, true);
foreach ($twitter_data as $elem)
{
print_r($elem);
echo '<br>';
}
Error returned on page
Array ( [0] => Array ( [message] => Could not authenticate you [code] => 32 ) )
Answer seemed to be that the line
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk";
should not have the ?screen_name=jameskrawczyk at the end.
You have to use an library to get data from twitter
"themattharris" is a good and famous library download it from here

Show facebook, twitter status updates in PHP

in my webpage, I want to show facebook and twitter status updates of my account, as one. Means show statuses from twitter and fb date wise. As shown below:
T: twitter status update 1
T: twitter status update 2
F: Facebook status update 3
T: twitter status update 4
F: Facebook status update 5
I am using wordpress for my website, is there any such plugin in wordpress? or any code using which I can do it manually in php?
Please check and advise.
Thanks!
Here is php code for twitter update/status.
$body = array(
'status'=>'YOURMESSAGE'
);
$url = "https://api.twitter.com/1.1/statuses/update.json";
$oauth_access_token = "YOURVALUE";
$oauth_access_token_secret = "YOURVALUE";
$consumer_key = "YOURVALUE";
$consumer_secret = "YOURVALUE";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$base_info = buildBaseString($url, 'POST', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$result_data = json_decode($json);
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
`

Categories