Reputation: 35
I have a textarea on my website that a user can edit, with a maximum of 140 characters. I have two submit buttons - one to post to Facebook, one for posting to Twitter. I was able to get the Facebook button working okay, but I'm stuck halfway with the Twitter button.
At the moment when a user clicks on the button to tweet, I can only get it to tweet to my own account. I want this to go to the user's account. I realize this is because I define my oAuthToken / Secret in my PHP, but I am not sure how to request for a user sign in / authorize my app and therefore creating and using their oAuth info. I can't seem to find an answer that works for me here or anywhere else so some help or a point in the right direction would be great!
Here's my code so far:
<?php if($_SERVER['REQUEST_METHOD'] == "POST") { ?>
<?php $statusupdate = $_POST['text']; ?>
<?php $socialsubmit = $_POST['socialsubmit']; ?>
<?php if($socialsubmit == "share") { ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '182922938522212', // App ID
channelUrl : 'channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.ui({
method: 'feed',
name: 'Minty Delivers',
caption: 'Same Day Grocery Delivery in Halifax',
description: '<?php echo $statusupdate; ?>',
link: 'http://www.mintydelivers.com',
picture: 'http://www.mintydelivers.com/connect-share.png'
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<?php echo $socialsubmit;?>
<?php } else if($socialsubmit == "tweet") { ?>
<?php
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$oAuthToken = 'xxx';
$oAuthSecret = 'xxx';
require_once('tw/twitteroauth.php');
// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//send a tweet
$tweet->post('statuses/update', array('status' => $statusupdate));
?>
<div class="alert alert-success">
Your tweet was successfully posted. Thanks for sharing!
</div>
<?php } ?>
<?php } ?>
<form id="status-update" action=""; method="post">
<textarea name="text" onKeyPress="return charLimit(this)" onKeyUp="return characterCount(this)">I found this GREAT company called Minty that delivers groceries, wine, flowers and gifts anywhere in Halifax! www.mintydelivers.com</textarea>
<p><span id="charCount">9</span> characters left</p>
<button id="twitter" class="tweetpopup" name="socialsubmit" value="tweet" type="submit">Tweet This</button>
<button id="facebook" name="socialsubmit" value="share" type="submit">Share This</button>
</form><!-- end form -->
Upvotes: 2
Views: 3374
Reputation: 2521
The user needs to authorize your application to tweet on their behalf. This involves redirecting them to Twitter and having them grant access to your application. The twitter docs on this topic are here:
https://dev.twitter.com/docs/auth/obtaining-access-tokens
One simple way to implement this behavior in PHP is via Temboo's OAuth wrappers. Temboo breaks the OAuth process into two calls, Initialize
and Finalize
, the first of which returns the Twitter authorization URL that you need to direct your users to. The second step of the process returns the access tokens you need to tweet on behalf of your users. You can test Twitter OAuth from Temboo's website and then use it in your code via the Temboo PHP SDK.
https://www.temboo.com/library/Library/Twitter/OAuth/
(Full disclosure: I work at Temboo)
Upvotes: 1