CURL删除对Instagram API的请求失败

时间:2012-12-29 15:57:04

标签: javascript php curl instagram http-delete

我试图通过调用API来尝试不同于我在Instagram上喜欢的照片。我的网址与instagrams API工具中的网址完全一样。我正在使用Curl。我得到一个空的回复,没有错误或状态代码。

这是我的"喜欢/不喜欢"用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;
}

我喜欢的代码完美运作......任何想法? 根据文档,DELETE请求应如下所示:

curl -X DELETE https://api.instagram.com/v1/media/{media-id}/likes?access_token=MYACCESSTOKEN

1 个答案:

答案 0 :(得分:1)

问题解决了。在我的PHP删除方法中,$ url以url=...开头,我只需删除它。