1

我是 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;
}

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

4

3 回答 3

6

您需要对返回的数据进行回显或执行某些操作(此外$,您的 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->location->name

如果你想检查它是否为空,如果它是空的,那么不要显示它,但如果它被设置,你还想在它上面附加一个字符串,你会做类似的事情:

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

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

于 2012-04-19T15:49:38.297 回答
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 回购

于 2015-02-13T18:21:24.907 回答
0

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

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

与 foreach 语句一致:foreach ($jsonData->data as $key=>$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>';
     }

  }
?>
于 2013-08-22T22:30:16.037 回答