处理 Instagram API (JSON + PHP) [英] Dealing with Instagram APIs (JSON + PHP)

查看:26
本文介绍了处理 Instagram API (JSON + PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我迄今为止创建的内容:

data;$result = '
    ';foreach ($data as $key => $value) {$result .='

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

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

试试这个:

'.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;返回 $result;}回声 get_instagram();?>

<小时>使用 $value->location->name

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

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

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

I'm new to PHP and I can't figure out how to deal with Instagram APIs in order to (for example) extract a list of links to the standard resolution images by recent 3 items published by the user_id 3.

Here's what I created till now:

<?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;
}

The result is a blank page though.. can you help me?

解决方案

You need to echo or do something with the returned data (Also you have a rouge $ in front of your json_decode function)

Try this:

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


With $value->location->name

If you want to check its empty and if it is then dont show it but also want to append a string onto it if it is set you would do something like:

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

Then use $location to echo where you want it.

这篇关于处理 Instagram API (JSON + PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆