Jump to content

POST to Twitter with JS calls


Mad_Griffith

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;}
Link to comment
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
Link to comment
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);});
Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
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
×
×
  • Create New...