Search code examples
phpcurlinstagram-api

how to post a comment with instagram API on PHP


i want to ask something about instagram API. i have try this API and get some problem about "Comment" section.

how can i post i comment on instagram API with php curl? on API documentation clearly explain that i must use this code :

curl -F 'access_token=ACCESS-TOKEN' \
 -F 'text=This+is+my+comment' \
 https://api.instagram.com/v1/media/{media-id}/comments

but, i have no idea or a sample for execute this with php. please teach me or give me a sample for using php curl.

any help would be appreciate, thankyou very much :)

update code :

<form method="post" action=''>
<p><em>Media id</em> <input type="text" name="tag" placeholder="Awesome"/><br>
<input type="submit" value="Ok" /></p>
</form>

<?php


  if(!empty($_POST['tag'])){


    $url = 'https://api.instagram.com/v1/media/1285310544400585898_1388393123/comments';

try {
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query(array('access_token' => 'myaccesstoken','text'=>' test')));
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

//Data are stored in $data
$data = json_decode(curl_exec($curl_connection), true);
print_r($data);
curl_close($curl_connection);
} catch(Exception $e) {
return $e->getMessage();
}

}
?>

Solution

  • use this function to send a POST request

    function httpPost($url, $data)
    {
        $curl = curl_init($url);
    
    
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
    

    httpPost('https://api.instagram.com/v1/media/{media-id}/comments', ['access_token' => 'your access token', 'text' => 'your comment'])