Is this possible to send tweet from application to user wall? - php

I have registered an application in Twitter. Now I want to send every update to user from application in the form of tweets using php :
$consumer_key = 'XXXXX';
$consumer_secret = 'XXXXX';
$oauth_token = 'XXXXX';
$oauth_secret = 'XXXXX';
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_secret);
$connection->host = "https://api.twitter.com/1.1/";
$connection->ssl_verifypeer = TRUE;
$parameters = array('status'=>'this is a message');
$status = $connection->post('statuses/update',$parameters);
The code is working fine but it is posting tweet from user's own account to his wall which is not what I want. I want to send tweet from application to user's wall.

Related

post a tweet to multiple accounts php

am trying to create a php script that post tweets to some users
i use that php class to do that
https://github.com/abraham/twitteroauth
but how can i post a tweet to multiple accounts in the same time
i use this code but it sand to one account only
<?php
session_start();
require_once('library/twitteroauth.php');
$ConsumerKey = "xxx";
$ConsumerSecret = "xxx";
$oauth_token = "xxx";
$oauth_token_secret = "xxx";
$connection = new TwitterOAuth($ConsumerKey, $ConsumerSecret ,$oauth_token , $oauth_token_secret);
//$twit_body = $_POST['twit_body'];
$twit_body = 'تجربة للجميع022222222';
$status = $connection->post('statuses/update', array('status' => $twit_body) );
//$status = $connection->send($twit_body);
print_r($status);
?>
You need different oAuth Tokens to achieve this. They are unique for each user.
Please go through this link: Post to multiple twitter accounts PHP
You'd need different oauth tokens, they are assigned on a per-user basis.

Post tweet option using php only working for me, how to make it general?

Hi all i developed an application for posting tweet using PHp with twitter api 1.1. But that option is only working for me only. If any one authenticated and try to send tweet using that. It's posting tweet on my wall.
How to make this generalized for anyone.
YOUR_CONSUMER_KEY = 'xxxxxxxxxxxxxx';
YOUR_CONSUMER_SECRET = 'xxxx';
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken('http://xxxx/xxxx/getTwitterData.php');
//print_r($request_token);
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
$tmessage = $_POST['message'];
$content = $twitteroauth->post('statuses/update', array('status' => $tmessage));
it's posting tweets on your wall because you're using access token and secret of the app, or you're the authenticated user. You need to log in the user you want to post for, get their access token and secret, then use consumer key, secret, user access token and user access secret to post on their behalf.
It's a bit unclear what you're trying to do, but here's a sample post action with Abraham William's library, which you're using:
require_once('twitteroauth.php');
$key = "***";
$secret = "***";
$token = "***";
$token_secret = "***";
$connection = new TwitterOAuth($key, $secret, $token, $token_secret);
$message = "whatever";
$status = $connection->post($message);
$response= $connection->http_code;
if($response !=200){
echo "ERROR";
}else{
echo "life is good";
}

Facebook PHP Sdk Access Token

I know there are tons of topics
on this question, but every single one that I've seen, seems to have no solution at all!
I'm getting the infamous "Oauth Exception - An active access token must be used to query information about the current user" and I don't know what to do.
My facebook connect was working perfectly fine until yesterday, now, it stopped working all of a sudden today.
Can anyone please help me with this?
I'm using php sdk to connect the user to facebook in a pop-up window, then I make him autorize my app and redirect the window to a php called facebookReturn.php.
In this file, I'm supposed to get the user info, save it on Session, then close the window and refresh the main page behind.
Here are my codes.
index.php:
require_once 'phps/facebook.php';
$config = array();
$config[appId] = 'xxxx';
$config[secret] = 'xxxx';
$config[fileUpload] = false;
$facebook = new Facebook ($config);
$facebookId = $facebook->getUser();
echo "<br>facebook id: $facebookId";
if ($facebookId) {
$userProfile = $facebook->api('/me');
$logoutUrl = $facebook->getLogoutUrl();
}
else {
$params = array ();
$params[scope] = 'publish_stream, user_birthday, email, user_activities';
$params[redirect_uri] = 'http://www.mydomain.com.br/phps/facebookReturn.php';
$params[display] = 'popup';
$loginUrl = $facebook->getLoginUrl($params);
}
facebookReturn.php:
require_once 'facebook.php';
$config = array();
$config[appId] = 'xxxx';
$config[secret] = 'xxxx';
$config[fileUpload] = false;
$facebook = new Facebook ($config);
$user = $facebook->getUser();
$userProfile = $facebook->api('/me');
Thank you in advance.
Remomber: Facebook token expires in 60 days.

Tweet multiple accounts using OAuth in twitter using PHP?

I wanted an application wherein I have a tweet textbox and when the user click on the submit button the tweet will be posted to multiple twitter account.
I already made this working but only on a single account. Using this example: https://dev.twitter.com/docs/auth/oauth/single-user-with-examples
I also have this code:
function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken('myAuthTokenInAppDetail', 'myAuthTokenSecretInAppDetail');
$content = $connection->post('statuses/update', array('status'=>'hello world') );
This code works when the $oauth_token and $oauth_token_secret values are the one that can be found in my App details( https://dev.twitter.com/apps/1482596/show ), but when I use a different twitter account, it will go the the Authorize Page then Redirect Back to my application and says:
stdClass Object
(
[error] => Could not authenticate you.
[request] => /1/statuses/update.json
)
This is my code when this happens, I just get the $_SESSION that was being generated once it got back to my application:
function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
return $connection;
}
$oauth_token = $_SESSION['oauth_token'];
$oauth_token_secret = $_SESSION['oauth_token_secret'];
$connection = getConnectionWithAccessToken($oauth_token, $oauth_token_secret);
$content = $connection->get("statuses/home_timeline");
$content = $connection->post('statuses/update', array('status'=>'hello world') );
I don't know why this happens. Kindly share some idea how to implement that app that I wanted. Any idea will be greatly appreciated and rewarded! Thanks! :)
If this is #Abraham's TwitterOAuth and you're using the default callback.php, you'll need to change your session lines to:
$oauth_token = $_SESSION['access_token']['oauth_token'];
$oauth_token_secret = $_SESSION['access_token']['oauth_token_secret'];
just have to mention oauth_token and oauth_token_secret in App details are called TokenKey and Tokensecret and for the new users you should get back $oauth_token and $oauth_token_secret from twitter API after they click on authorization link
if ( empty($_GET["oauth_token"]) && empty($_SESSION['oauth_token']) )
//you could add another conditions here using cookies or using DB .
{
$request_token = $connection->oauth('oauth/request_token',
array('oauth_callback' => OAUTH_CALLBACK));
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
echo " <br/><a href='$url'>Sign-in</a> <br/>";
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
}
else {
$oauth_verifier = $_SESSION['oauth_token'] ;
$oauth_token = $_SESSION['oauth_token_secret'] ;
// return user details
$request_token2 = $connection->oauth('oauth/access_token',
array( 'oauth_verifier' => $oauth_verifier , 'oauth_token' => $oauth_token ) ) ;
$connection2=new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
$request_token2[oauth_token] , $request_token2[oauth_token_secret] );
// send out other user new status .
$statues = $connection2->post("statuses/update", ["status" => "hello world"] );
}
of course you can improve that code using other function condensations and add DB table for each new authorized user ,
but keep in mind requesting a new request_token will generate new oauth_token & oauth_token_secret keys each time .
last one of most thing confused me when post other user status CONSUMER_KEY, CONSUMER_SECRET twitter uses the same key for main app authorization and other users authorization , i know some people still fall into this .

How to send direct messages using OAuth?

I am trying to make a twitter application. I have gotten my application to a point where users log in through Twitter's user verification system. I am also able to send status updates using my application. I have this function to send status updates:
function sendTweet($tx){
$consumer_key = 'MY CONSUMER KEY';
$consumer_secret = 'MY CONSUMER SECRET';
$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($_SESSION['OT'],$_SESSION['OTS']);
$text=$tx;
$status=$Twitter->post_statusesUpdate(array('status' => $text));
$status->response;
}
This sendTweet($tx) function works like a charm but now what i need to do is get a list of followers of a user and then send Direct Messages to them. How can I do this using oauth and PHP?
function sendDirectMessage($user,$message)
{ $consumer_key = 'YOUR CONSUMER KEY';
$consumer_secret = 'YOUR SECRET';
$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($_SESSION['OT'],$_SESSION['OTS']);
$myArray = array('user' => $user, 'text' => $message);
$resp = $Twitter->post_direct_messagesNew( $myArray);
$resp->response;
}

Categories