phpapicurlinstagramsocial-media-like

Instagram POST "like" in PHP isn't working any more?


Today I tried to post a like via PHP (Curl) without luck. The output is that a token is required but I used a working token. I tried the same token with JS and it works.

Did Instagram changed some things bout PHP?

Here is my code:

<?php


    $media_id = '615839918291043487_528338984';

    $url = "https://api.instagram.com/v1/media/615839918291043487_528338984/likes?";
    $access_token_parameters = array(
        'access_token'       =>      '191573449.9e262d9.ff708911edcd4f809ca31dd76d08c0ba',
        'action'             =>      'like'
    );

    $curl = curl_init($url);

    curl_setopt($curl,CURLOPT_GET,true);

    curl_setopt($curl,CURLOPT_GETFIELDS,$access_token_parameters);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    echo curl_exec($curl);

?>

Output.:

{"meta":{"error_type":"OAuthParameterException","code":400,"error_message":"Missing client_id or access_token URL parameter."}}

I tried it on several server, with several proxies, several client and token. Hopefully you know what's going on.


Solution

  • To add a like to a photo you need to do it via POST. The following is an modified version of your code to do this.

    <?php
    
        $url = "https://api.instagram.com/v1/media/615839918291043487_528338984/likes";
        $fields = array(
            'access_token'       =>      '191573449.9e262d9.ff708911edcd4f809ca31dd76d08c0ba'
        );
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
    
        echo $response;
    ?>