Вопрос

I'm trying to unlike a photo that I just liked on Instagram with a call to the API. My url is exactly as the URL in instagrams API tool. Im using Curl. I get an empty response, no error or status code.

Here is my "like/unlike" method written in 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');
        }
    });

And here is my Server Code written in 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;
}

My Like code works Perfectly... any thoughts? According to the documentation a DELETE request should look like this:

curl -X DELETE https://api.instagram.com/v1/media/{media-id}/likes?access_token=MYACCESSTOKEN
Это было полезно?

Решение

Problem solved. In my PHP delete method, $url started with url=... I just had to remove that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top