首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理Instagram API (JSON + PHP)

处理Instagram API (JSON + PHP)
EN

Stack Overflow用户
提问于 2012-04-19 23:22:20
回答 3查看 14.6K关注 0票数 1

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

下面是我之前创建的内容:

代码语言:javascript
复制
<?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;
}

结果是一张空白的页面..你能帮帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-19 23:49:38

您需要回显返回的数据或对返回的数据执行某些操作(在json_decode函数前面还有一个rouge $ )

试试这个:

代码语言:javascript
复制
<?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->location->name

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

代码语言:javascript
复制
$location = (!empty($value->location->name))?'@'.$value->location->name:null;

然后使用$location在您想要的位置回显。

票数 6
EN

Stack Overflow用户

发布于 2015-02-14 02:21:25

我已经通过编写一个小脚本来扩展选择的答案,任何人都可以使用它作为样板。

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

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

代码语言:javascript
复制
<?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>';
    }
}

基本用法

代码语言:javascript
复制
<?php echo instagram(); ?>

高级用法

代码语言:javascript
复制
<?php echo instagram($count = 1, $width = 100, $height = 100); ?>

Github Repo

票数 2
EN

Stack Overflow用户

发布于 2013-08-23 06:30:16

嗯,这在一周前还能用,但是现在,它给了我一个PHP错误:

“消息:正在尝试获取非对象的属性”

在foreach语句行上:foreach ($jsonData->data as $key=>$value)

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

代码语言:javascript
复制
<?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>';
     }

  }
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10231369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档