How to Call Twitter API via an AJAX/PHP button using curl?


#1

Hi,

I’m trying to build a script that follows/unfollows people by pressing an AJAX button.
Let’s say I have 3 files:
tweeps.php - just a list of people with two buttons each - Follow/Unfollow
follow.php
unfollow.php

The latter two contain curl classes that call Twitter API and follow/unfollow specified people.

The trick is to remain on tweeps.php without having to navigate away to follow.php and unfollow.php so that people on the list simply disappear when unfollowed.

The problem is with curl, cross domain request can’t be done via AJAX.

Can you please suggest the best way of doing it?

Here’s something I tried:

var request = $.ajax({
  url: "https://api.twitter.com/1.1/friendships/create.json?callback=?",
  type: "GET",
  data: JSON.stringify({ "screen_name" : tweep }),
  dataType: "json",
  contentType: "application/json", success: function(data) {
        alert("ok");
        //debugger;
    },
    error: function(data) {
        alert("not ok");
       // debugger;
    }
  
});
 
request.done(function( msg ) {
  alert('success');
  document.getElementById(tweep).innerHTML='<img src="http://example.com/images/inactive.png" />';
     
});
 
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});

Thanks!