Problems to post a tweet with php - php

I'm using this code to get the last 20 tweets I was mentioned in.
But I can't adapt it to be able to tweet a simple text tweet. can someone help me?
I tried everything but i'm stuck.
I dont know what to do...
I know I have to post to /update.json , and use update -> text
but its not working
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;
}
function returnTweet(){
$oauth_access_token = "xxxxxxxxxxxxxxxx";
$oauth_access_token_secret = "xxxxxxxxxxxxxxxx";
$consumer_key = "xxxxxxxxxxxxxxxx";
$consumer_secret = "xxxxxxxxxxxxxxxx";
$timeline = "mentions_timeline";
// create request
$requestrt = array(
'screen_name' => 'myScreenName',
'extended_entities' => 'true',
'exclude_replies' => 'true',
'include_rts' => 'false',
'tweet_mode' => 'extended',
'count' => '20');
$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'
);
// merge request and oauth to one array
$oauth = array_merge($oauth, $requestrt);
$base_info = buildBaseString("https://api.twitter.com/1.1/statuses/$timeline.json", '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 request
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$timeline.json?". http_build_query($requestrt),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return json_decode($json, true);
}
$tweet = returnTweet();

Related

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 .

Validate twitter usename using twitter api version-1.1

I want to validate twitter usename. I referred this Link
It returns the twitter json array when i tried in local (XAMPP) server. But it returns empty in server.
FYI: CURL is enabled in the server.
How to solve this.
Updates:
protected 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));
}
protected function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
public function returnTweet(){
$oauth_access_token = "myToken";
$oauth_access_token_secret = "MySecrekKey";
$consumer_key = "My Consumer Key";
$consumer_secret = "My Consumer Secret Key";
$twitter_timeline = "user_timeline";
$twusername = 'myUsername';
// create request
$request = array(
'screen_name' => $twusername,
'count' => '3'
);
$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'
);
$oauth = array_merge($oauth, $request);
$base_info = Mage::getModel('Namespace/Filename')->buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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(Mage::getModel('socialtabs/SocialTab')->buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}

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

twitter API 1.1: cannot add the 'count' parameter using curl & php

I'm connecting to the new twitter API to get a list of my profiles' public tweets.
I'm basically using the following code, which I found here: Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
I'm getting the last 20 tweets and I've tried many different ways to add the "count" parameter to the curl call, but nothing seems to return more results.
I tried giving a value to the $postfields:
$postfields = array(
'count' => 100,
'include_rts' => 1
);
//url-ify the data for the POST
foreach($postfields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
Giving a simple string value:
§postfields = 'count=100';
and changing the $url value into:
$url = "https://api.twitter.com/1.1/statuses/home_timeline.json?count=100";
Any other suggestions?
Thanks for your help!!
This is the complete code for reference:
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";
$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, '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);
$items = '15';
$screen_name = 'YOUR SCREEN NAME';
/* the $oauth */
$oauth = array(
'screen_name' => $screen_name,
'count' => $items,
'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 . '?screen_name='.$screen_name.'&count='.$items,
CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false
);

Categories