我试图通过调用 API 来改变我在 Instagram 上喜欢的照片。我的网址与 instagrams API 工具中的网址完全相同。我使用卷曲。我得到一个空响应,没有错误或状态代码。
这是我用 javascript 编写的“喜欢/不喜欢”方法。
likeImage: function(type, id)
{
var params = "url=https://api.instagram.com/v1/media/" + id + "/likes";
if(type === 'DELETE')
{
params += "?access_token=" + localStorage.getItem('id') + "." + localStorage.getItem('token');
}
else
{
params += "&access_token=" + localStorage.getItem('id') + "." + localStorage.getItem('token');
}
$.ajax({
url: "crossDomain.php",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
type: type,
data: params,
beforeSend: function ()
{
console.log('Start' + type);
},
complete: function(data)
{
console.log('Finished ' + type);
},
success: function(data)
{
console.log('Success ' + type);
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log('JQXHR:' + jqXHR);
console.log('TEXTSTATUS: ' + textStatus);
console.log('ERROR THROWN:' + errorThrown);
console.log('error');
}
});
这是我用 PHP 编写的服务器代码
$method = $_SERVER['REQUEST_METHOD'];
if($method == "POST")
{
$url = $_POST['url'];
$token = $_POST['access_token'];
$fields = array(
"access_token" => urlencode($token)
);
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
else if($method == 'DELETE')
{
$url = file_get_contents('php://input');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
我的 Like 代码完美运行……有什么想法吗?根据文档,删除请求应如下所示:
curl -X DELETE https://api.instagram.com/v1/media/{media-id}/likes?access_token=MYACCESSTOKEN