如何使用Instagram API发布喜欢或删除Instagram


how to post a like or delete an instagram like using instagram api?

大家好,任何人都可以告诉我如何使用 API 发布喜欢和删除 Instagram?我试图使用此代码删除喜欢,但我没有得到 Ajax 调用的任何响应。谁能告诉我我在这里做错了什么?这可能做PHP吗?如何?

xxxxxxxxxxxxxxxxxx_xxxxxxxx ==> 媒体 ID,例如:23424343243243_2343243243

用于删除喜欢的Instagram API文档

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

编辑:跨域 php curl:(但如何判断这是 post 方法还是删除方法?

$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;
}

编辑:根据 API 规范,您需要使用DELETE类型的请求。为此,请指定{type: "DELETE"} ,而不是{"method": "delete"}并使用$.ajax()函数。请参阅此处的相关问题:如何在jQuery中发送PUT/DELETE请求?

您确定您的 POST 请求已被执行吗?它可能根本没有运行,请检查开发人员工具/Firebug/等中的请求。并初始化你的请求jQuery方式,不要使用突兀的JS:

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

使用参数的方式success您需要声明具有更多参数的函数:success(data, textStatus, jqXHR) 。这也可能导致不当行为。请在此处阅读文档:http://api.jquery.com/jQuery.post/

我建议您使用更方便的.done()功能:

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

您也绝对应该有兴趣检查.fail()结果 - 很可能您在准备请求时错过了一些东西。