0

hi all could any one tell me how i can post a like and delete an instagram like using api? i tried to delete a like using this code but i dont get any response from ajax call. could any one tell me what i am doing wrong here ?is this possible doing php ?how?

xxxxxxxxxxxxxxxxxx_xxxxxxxx ==> Media-Id such as: 23424343243243_2343243243

Instagram API Docs for Deleting Like

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<script type="text/javascript">
function deleteLike() {

alert("inside function");

    var url = "https://api.instagram.com/v1/media/xxxxxxxxxxxxxxxxxx_xxxxxxxx/likes?access_token=XXXX";
    $.post(url, {
        "method": "delete",
    }, function(data) 
       {
         var ajaxResponse = data;

          alert("success"+ajaxResponse);

    })
    }

</script>
</head>
  <body onload="deleteLike();">


</html>

Edited:cross domain php curl:(but how to tell it if this is post method or delete method?"

$url = "https://api.instagram.com/v1/media/xxxxxxxxxxxxxxxxxx_xxxxxxxx/likes?access_token=XXXX";

        $api_response = get_data(''.$url);
        $record = json_decode($api_response); // JSON decode 



/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • You're doing a cross-domain request. This is **NOT** permitted in browser-based javascript. Code loaded from server A can only ever make AJAX requests back to server A. – Marc B Sep 17 '13 at 14:45
  • @MarcB There's tons of examples of using AJAX to access the Instagram API. Are you sure? – Reinstate Monica Cellio Sep 17 '13 at 14:48
  • 1
    yes, but they'll be either using JSONP to bypass the cross-domain restriction, or using ajax JS code loaded from instagram directly. As written above, the JS code is coming from the jquery cdn, and the containing page is loaded from the OP's own site. It's not possible for the ajax code to poke at instagram in this configuration, EXCEPT if it was a jsonp request. – Marc B Sep 17 '13 at 14:49
  • thanks for replies. I think it is cross domain issue. How i can make curl delete request to api from within my own php script and call that php from my ajax? i dont know how to make curl call and tell curl if it is post or delete reqeust!could you tell me how ? – user1788736 Sep 17 '13 at 14:50
  • @MarcB - yes - the magic letter P is exactly how it's done. Thanks :) – Reinstate Monica Cellio Sep 17 '13 at 14:53
  • 1
    @user1788736: use CURLOPT_CUSTOMREQUEST to set the 'verb' to use: http://php.net/manual/en/function.curl-setopt.php – Marc B Sep 17 '13 at 14:54
  • so you mean i use: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); ? – user1788736 Sep 17 '13 at 15:05

1 Answers1

0

EDIT: According to API specs, you need to use request of DELETE type. To do this, specify {type: "DELETE"}, not {"method": "delete"} and use $.ajax() function. See related question here: How to send a PUT/DELETE request in jQuery?

Are you sure you've got your POST request gets executed at all? It may be not running at all, please check requests in Developer Tools / Firebug / etc. And initialize your request jQuery way, don't use obtrusive JS:

$(function(){
    deleteLike();
});

The way you use success parameter you need to declare functions with more parameters: success(data, textStatus, jqXHR). This may also cause misbehavior. Please read docs here: http://api.jquery.com/jQuery.post/

I'd suggest you to use more handy .done() function:

$.post(url, { "method": "delete"})
  .done(function(data) {
    var ajaxResponse = data;
    alert("success: " + ajaxResponse);
  })
  .fail(function() {
  // check for error here
  });

You should also definitely be interested in checking .fail() result - most probably you've missed something while preparing request.

Community
  • 1
  • 1
keaukraine
  • 5,315
  • 29
  • 54