如何通过 API 使用 PHP 获取 Instagram 照片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48473687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:48  来源:igfitidea点击:

How to get instagram photos with PHP by API?

phpinstagraminstagram-api

提问by Tanmoy Biswas

Last two days I was trying to display photos from my Instagram profile by raw PHP. But I'm getting these errors.

前两天我试图通过原始 PHP 显示我的 Instagram 个人资料中的照片。但我收到这些错误。

Notice: Trying to get property of non-object in E:\xampp7\htdocs\instagram\index.php on line 29

Warning: Invalid argument supplied for foreach() in E:\xampp7\htdocs\instagram\index.php on line 29

I created proper authenticationfrom instagram API. My requested API clients accessis valid (clientId, userId, accessToken). When I failed with PHP than I tried with a JavaScript Plugin instafeedjs. Now It's working well. Here is my PHP code, given below:-

我从 Instagram API创建了正确的身份验证。我请求的 APIclients access有效 ( clientId, userId, accessToken)。当我使用 PHP 失败时,我尝试使用 JavaScript 插件instafeedjs。现在它运行良好。这是我的 PHP 代码,如下所示:-

<body>
<?php
function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    $result = fetchData("https://api.instagram.com/v1/users/3550421370/media/recent/?access_token=MY_VALID_ACCESS_TOKEN_IS_HERE");

    $result = json_decode($result);
    foreach ($result->data as $post) {
    if(empty($post->caption->text)) {
    // Do Nothing
    }
    else {
    echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
        <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" />
        <div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
    }
}

?>

</body>

回答by Tanmoy Biswas

I get the solution. It is simple by using JSON data as a multi-dimensional array. Every kind of data is able in this array.

我得到了解决方案。使用 JSON 数据作为多维数组很简单。每种数据都可以在这个数组中。

<?php
$access_token = "My_Access_Token";
$photo_count = 9;
$json_link = "https://api.instagram.com/v1/users/self/media/recent/?";
$json_link .="access_token={$access_token}&count={$photo_count}";
$json = file_get_contents($json_link);
$obj = json_decode(preg_replace('/("\w+"):(\d+)/', '\1:"\2"', $json), true);
echo "<pre>";
print_r($obj);
echo "</pre>";
foreach ($obj['data'] as $post){
    $pic_text = $post['caption']['text'];
    $pic_link = $post['link'];
    $pic_like_count = $post['likes']['count'];
    $pic_comment_count=$post['comments']['count'];
    $pic_src=str_replace("http://", "https://", $post['images']['standard_resolution']['url']);
    $pic_created_time=date("F j, Y", $post['caption']['created_time']);
    $pic_created_time=date("F j, Y", strtotime($pic_created_time . " +1 days"));
    echo "<div class='col-md-4 item_box'>";
        echo "<a href='{$pic_link}' target='_blank'>";
          echo "<img class='img-responsive photo-thumb' src='{$pic_src}' alt='{$pic_text}'>";
        echo "</a>";
    echo "<p>";
    echo "<p>";
        echo "<div style='color:#888;'>";
            echo "<a href='{$pic_link}' target='_blank'>{$pic_created_time}</a>";
        echo "</div>";
    echo "</p>";
    echo "<p>{$pic_text}</p>";
    echo "</p>";
    echo "</div>";
}
?>

回答by Hasan Shahriar

Your API endpoint is wrong. This is the API to fetch recent media of an userfrom Instagram:

您的 API 端点是错误的。这是从 Instagram获取用户最近媒体的 API :

https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN

https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN

Get the most recent media published by the owner of the access_token.

获取 access_token 所有者发布的最新媒体。

Parameters

参数

access_tokenA valid access token.

访问令牌A valid access token.

max_idReturn media earlier than this max_id.

最大idReturn media earlier than this max_id.

min_idReturn media later than this min_id.

min_idReturn media later than this min_id.

countCount of media to return.

数数Count of media to return.

Read more: https://www.instagram.com/developer/endpoints/users/#get_users_media_recent_self

阅读更多:https: //www.instagram.com/developer/endpoints/users/#get_users_media_recent_self