如何在PHP上发布带有Instagram API的评论

时间:2016-07-02 10:42:07

标签: php curl instagram-api

我想询问有关Instagram API的一些信息。我已经尝试了这个API并且在“评论”部分遇到了一些问题。

我怎么能用php curl发布我对Instagram API的评论?关于API文档清楚地解释我必须使用此代码:

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

但是,我不知道或者用php执行此操作的示例。请教我或给我一个使用php curl的样本。

任何帮助都会很感激,非常感谢:)

更新代码:

<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();
}

}
?>

1 个答案:

答案 0 :(得分:0)

使用此功能发送POST请求

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'])