处理Instagram API(JSON + PHP)

Mul*_*gno 1 php api json instagram

我是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;
}
Run Code Online (Sandbox Code Playgroud)

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

Law*_*one 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();
?>
Run Code Online (Sandbox Code Playgroud)


$value->location->name

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

$location = (!empty($value->location->name))?'@'.$value->location->name:null;
Run Code Online (Sandbox Code Playgroud)

然后用$location它来回显你想要的地方.