![](/img/trans.png)
[英]how to convert the instagram curl post request to javascript request?
[英]CURL Delete request to Instagram API fails
我試圖通過調用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代碼可完美運行...有什么想法嗎? 根據文檔,DELETE請求應如下所示:
curl -X DELETE https://api.instagram.com/v1/media/{media-id}/likes?access_token=MYACCESSTOKEN
問題解決了。 在我的PHP刪除方法中,$ url以url=...
開頭,我只需要刪除它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.