Jump to content

Twitter API: JS and PHP


Mad_Griffith

Recommended Posts

I am not sure I understand the flow correctly, though:

 

I post the blob appended to the form, from main.js (referenced in the index page) to tweet-with-media.php.

 

The tweet-with-media.php starts the session and checks whether the access token was retrieved.

 

If it was not received, it opens up a popup to callback.php where the access token is retrieved and stored in the session.

 

If the access token was already retrieved, it first saves the formData on the disk, then posts it to Twitter with a status, then deletes the picture from the disk.

 

I am not sure if it's okay what I am doing: splitting the process in 3 files and when should the popup be opened to have the user authorise the app.

 

I had

header('Location:' . $url);

which I replaced with

echo '<script>window.open("' . $url . '")</script>';

but the link is not opening in a new window. The response is:

<script>window.open("https://api.twitter.com/oauth/authorize?oauth_token=KbJJbAAAAAAAvciIAAABVRx51JM")</script>
Edited by Mad_Griffith
Link to comment
Share on other sites

I found out I have to use file_get_contents and now I actually see the picture saved.

 

The problem now is I don't know when to open the popup to ask for user's authorisation. Do I have to open it through JS? If so, on "success" or "beforeSend"? Or should this happen in the tweet-with-image.php file?

Also, do I have too many files? I have the index.html (where the user's interaction with the button is supposed to happen), then this tweet-with-image.php file and the callback.php file.

 

I would be grateful if you could make me understand how to handle this final part...

 

Thank you, I feel I am close! :)

Edited by Mad_Griffith
Link to comment
Share on other sites

for example this attempt at building the AJAX call doesn't work consistently...

 

$('#twitter').click(function() {


canvasToData();


$.ajax({
url: 'tweet-with-media.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
beforeSend: function() {
$('#twitter').addClass('twitter-beforesend');
},
success: function(data) {
console.log(data)
if (data.indexOf('https://api.twitter.com/oauth') > -1) {
window.open(data); // authorisation URL
$.ajax({
url: 'tweet-with-media.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
cache: false
})
}
},
complete: function() {
$('#twitter').removeClass('twitter-beforesend');
$('#twitter').addClass('twitter-success');
setTimeout(
function() {
$('#twitter').removeClass('twitter-success');
}, 3000);
}
});


});

In fact I receive this error:

<b>Notice</b>:  Undefined variable: request_token in <b>tweet-with-media.php</b> on line <b>31</b><br />
<br />
<b>Notice</b>:  Undefined property: stdClass::$media_id in <b>tweet-with-media.php</b> on line <b>45</b><br />
stdClass Object
(
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [code] => 89
                    [message] => Invalid or expired token.
                )


        )


)
Edited by Mad_Griffith
Link to comment
Share on other sites

I haven't looked at Twitter's API documentation, but you probably first open a window to have them sign in, and you get an authorization token back. Then you send that token back to Twitter on subsequent requests so that they know who the user is. It has to be explained in the documentation though.

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...