【问题标题】:Dealing with Instagram APIs (JSON + PHP)处理 Instagram API (JSON + PHP)
【发布时间】:2012-05-01 03:53:10
【问题描述】:

我是 PHP 新手,我不知道如何处理 Instagram API,以便(例如)通过 user_id 3 发布的最近 3 个项目提取标准分辨率图像的链接列表。

这是我到目前为止创建的内容:

<?php
function get_instagram($user_id,$count)
{
    $user_id = '3';
    $count = '3';
    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
    $jsonData = $json_decode((file_get_contents($url)));
    $data = $jsonData->data;
    $result = '<ul>';
    foreach ($data as $key => $value) {
        $result .= '<li><a href='.$value->link.' ><img src="'.$value->images->standard_resolution->url.'" width="70" height="70" /></a></li> ';
    }
    $result .= '</ul>';
    return $result;
}

结果是一个空白页..你能帮帮我吗?

【问题讨论】:

  • returning 结果。如果你想让它显示在页面上,你需要echo它。 (echo $result;)
  • 谢谢 nav_nav,我改了!无论如何,页面仍然是空白的..也许不仅仅是一个错误..? :(
  • 尝试回显 JSON 数据以查看来自 Instagram 的结果。 echo $jsonData;

标签: php json api instagram


【解决方案1】:

您需要对返回的数据进行回显或执行某些操作(另外,您的 json_decode 函数前面有一个胭脂 $

试试这个:

<?php
function get_instagram($user_id=15203338,$count=6,$width=190,$height=190){

    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;

    // Also Perhaps you should cache the results as the instagram API is slow
    $cache = './'.sha1($url).'.json';
    if(file_exists($cache) && filemtime($cache) > time() - 60*60){
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache));
    } else {
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }

    $result = '<div id="instagram">'.PHP_EOL;
    foreach ($jsonData->data as $key=>$value) {
        $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" 
                            title="'.htmlentities($value->caption->text).' '.htmlentities(date("F j, Y, g:i a", $value->caption->created_time)).'"
                            style="padding:3px" href="'.$value->images->standard_resolution->url.'">
                          <img src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" width="'.$width.'" height="'.$height.'" />
                          </a>'.PHP_EOL;
    }
    $result .= '</div>'.PHP_EOL;
    return $result;
}

echo get_instagram();
?>


$value-&gt;location-&gt;name

如果你想检查它是否为空,如果它是空的,那么不要显示它,但如果设置了它还想在它上面附加一个字符串,你可以这样做:

$location = (!empty($value->location->name))?'@'.$value->location->name:null;

然后使用$location 在你想要的地方回显。

【讨论】:

【解决方案2】:

我通过编写一个小脚本来扩展所选答案,该脚本可以用作任何尝试此操作的人的样板。

它设置了所有变量,并从基本标记开始。使用 Instagrams API 文档中的选择器,您可以根据需要构建其输出。

这是专为与 WordPress 一起使用而构建的,只需对图像大小稍作调整即可在任何 PHP 应用程序中使用它。

<?php
function instagram($count = 10, $width = 640, $height = 640) {

    $user_id = 123456789;
    $access_token = '123456789';
    $size = wp_is_mobile() ? 'low_resolution' : 'standard_resolution';
    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$access_token.'&count='.$count;
    $cache_location = './'.sha1($url).'.json';
    $cache_time = '-1 hour';

    if (file_exists($cache_location) && filemtime($cache_location) > strtotime($cache_time)) {
        // If a cache file exists, and it is newer than 1 hour, use it
        $jsonData = json_decode(file_get_contents($cache_location));
    } else {
        $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache_location,json_encode($jsonData));
    }

    foreach ($jsonData->data as $key=>$value) {
        echo '<div>';
        echo '<img src="'.$value->images->$size->url.'"/>';
        echo '</div>';
    }
}

基本用法

<?php echo instagram(); ?>

高级用法

<?php echo instagram($count = 1, $width = 100, $height = 100); ?>

Github Repo

【讨论】:

    【解决方案3】:

    Welp,这是一周前的工作,但现在,它给了我一个 PHP 错误:

    “消息:试图获取非对象的属性”

    在foreach语句的那一行:foreach ($jsonData-&gt;data as $key=&gt;$value)

    原来我的 JSON 提要实际上已损坏,我的一些 instagram 的标题丢失了,所以我不得不添加一个 if empty 语句。我现在也在使用不同的方法,很好奇你的想法,效果很好!

    <?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/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14");
    
      $cache = './instagram.json';
      if(file_exists($cache) && filemtime($cache) > time() - 60*60){
      // If a cache file exists, and it is newer than 1 hour, use it
      $instaData = json_decode(file_get_contents($cache));
        } else {
      $instaData = json_decode((file_get_contents($result)));
        file_put_contents($cache,json_encode($instaData));
      }
    
      foreach ($instaData->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>';
         }
    
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      相关资源
      最近更新 更多