Mad_Griffith Posted October 26, 2015 Share Posted October 26, 2015 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 More sharing options...
justsomeguy Posted October 26, 2015 Share Posted October 26, 2015 You'll need to register with the API you're trying to use. Your account will have the keys and tokens assigned to it. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted October 26, 2015 Author Share Posted October 26, 2015 (edited) 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 October 26, 2015 by Mad_Griffith Link to comment Share on other sites More sharing options...
justsomeguy Posted October 26, 2015 Share Posted October 26, 2015 You need to specify the names and values that you're sending. e.g.: data: {image: base64img},There are several examples and other information here:http://api.jquery.com/jquery.ajax/ Link to comment Share on other sites More sharing options...
Mad_Griffith Posted October 26, 2015 Author Share Posted October 26, 2015 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 More sharing options...
justsomeguy Posted October 26, 2015 Share Posted October 26, 2015 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 More sharing options...
Mad_Griffith Posted October 27, 2015 Author Share Posted October 27, 2015 (edited) Ok, thanks. If I create a PHP function, how can I then access it through JS? Could you make a simple example? Thanks! Edited October 27, 2015 by Mad_Griffith Link to comment Share on other sites More sharing options...
justsomeguy Posted October 27, 2015 Share Posted October 27, 2015 Just the way you're doing, you send an ajax request to the server and tell it what to execute. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted November 4, 2015 Author Share Posted November 4, 2015 The response is the content of the whole PHP file... how can I actually execute, through AJAX, a PHP function that is inside such PHP file? Link to comment Share on other sites More sharing options...
thescientist Posted November 4, 2015 Share Posted November 4, 2015 Is PHP configured to run on your server? Link to comment Share on other sites More sharing options...
Mad_Griffith Posted November 4, 2015 Author Share Posted November 4, 2015 Yes, PHP/5.5.29 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 4, 2015 Share Posted November 4, 2015 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. Link to comment Share on other sites More sharing options...
Mad_Griffith Posted November 4, 2015 Author Share Posted November 4, 2015 (edited) 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 November 4, 2015 by Mad_Griffith Link to comment Share on other sites More sharing options...
justsomeguy Posted November 4, 2015 Share Posted November 4, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now