Jump to content
Mad_Griffith

POST to Twitter with JS calls

Recommended Posts

Hi, I am currently trying to implement the posting of a canvas image to a Twitter wall.Below, first you see the JS I came up with.What I can't figure out is how am I supposed to get the keys and tokens provided in the PHP function below that code. Such PHP function is contained in the uploadFile.php file.JS:

var base64img = canvas.toDataURL().split(',')[1];                $.ajax({                    url: "uploadFile.php",                    type: "POST",                    data: base64img,                    processData: false,                    contentType: "application/octet-stream",                }).done(function(respond) {                    alert(respond);                });

PHP:

function _microsite_last_tweets($search) {define('CONSUMER_KEY', 'my key');define('CONSUMER_SECRET', 'my key');define('ACCESS_TOKEN', 'my key');define('ACCESS_TOKEN_SECRET', 'my key');$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);$content = $connection->get("account/verify_credentials");if ($search[0] == '@' && strpos($search,' ') == false) {$user = substr($search, 1);$tweets = $connection->get("statuses/user_timeline", array("screen_name" => $user, "count" => "4"));}else {$search = urlencode($search);$tweets = $connection->get("search/tweets", array("q" => $search . '+exclude:retweets', "result_type" => "recent","count" => "4"));}return $tweets;}

Share this post


Link to post
Share on other sites

You'll need to register with the API you're trying to use. Your account will have the keys and tokens assigned to it.

Share this post


Link to post
Share on other sites

Hi juststomeguy, yes, all the keys and tokens are already in the script (I took them out from the code I pasted here for security reasons).

 

I am just wondering how I should practically write the $.ajax call to have the script work.

Edited by Mad_Griffith

Share this post


Link to post
Share on other sites

I updated the JS/jQuery as follows but I am having a hard time understanding how establish whether I am heading towards the right direction and what else I have to possibly add to the script. Twitter documentation's being poorly written may contribute to my confusion, to some extent.

 

var base64img = canvas.toDataURL().split(',')[1];$.ajax({url: "uploadFile.php",type: "POST",data: {image: base64img},processData: false,contentType: "application/octet-stream"}).done(function(respond) {alert(respond);});

Share this post


Link to post
Share on other sites

That will send the image data to the server in a post request, although you need to remove that processData option or change it (again, check the jQuery documentation). The PHP code you showed is for searching tweets though, so that wouldn't relate to sending an image.

Share this post


Link to post
Share on other sites

Just the way you're doing, you send an ajax request to the server and tell it what to execute.

Share this post


Link to post
Share on other sites

Is PHP configured to run on your server?

Share this post


Link to post
Share on other sites

You tell PHP what to do. Send variables in $_GET or $_POST and then write the PHP code to check for them and decide what to execute.

Share this post


Link to post
Share on other sites

Could it be that the ajax URL is pointing to a Drupal .module file (which indeed contains PHP code)?

 

edit: Ok justsomeguy, I will give it a try.

Edited by Mad_Griffith

Share this post


Link to post
Share on other sites

An ajax request is exactly the same as any other request to a PHP file, like just typing in the URL to a PHP file or submitting a form. The PHP code needs to decide what to do based on the information that was submitted in $_GET, $_POST, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×