与Instagram的API的(JSON + PHP)处理 [英] Dealing with Instagram APIs (JSON + PHP)
问题描述
我是新来的PHP,我无法弄清楚如何才能对付的Instagram的API(例如)最近由USER_ID 3. <公布的3项提取的链接标准分辨率的图像列表/ p>
下面是我创建至今:
&LT; PHP
功能get_instagram($ USER_ID,$计数)
{
$ USER_ID ='3';
$计数='3';
$ URL = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
$ jsonData = $ json_de code((的file_get_contents($网址)));
$数据= $ jsonData-&GT;数据;
$结果='&LT; UL&GT;';
的foreach($数据,$关键=&GT; $值){
。$结果='&LT;立GT;&LT; A HREF ='$值 - 方式&gt;的链接。 &GT;&LT; IMG SRC ='$值 - 方式&gt;图像 - &GT; standard_resolution-方式&gt;网址'WIDTH =70HEIGHT =70/&GT;&LT; / A&GT;&LT; /李&GT; ';
}
$结果='&LT; / UL&GT;';
返回$结果;
}
结果是一个空白页,但..你能帮助我吗?
您需要回声或做返回的数据(也有胭脂 $
在面前的东西您json_de code函数)
试试这个:
&LT; PHP
功能get_instagram($ USER_ID = 15203338,$计数= 6,$ WIDTH = 190,$高度= 190){ $ URL = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count; //同时也许你应该缓存结果作为Instagram的API是缓慢的
$缓存='./'.sha1($url).'.json';
如果(file_exists($缓存)及&放大器; filemtime($缓存)&GT;时间() - 60 * 60){
//如果缓存文件存在,它是较新的1个多小时,用它
$ jsonData = json_de code(的file_get_contents($缓存));
}其他{
$ jsonData = json_de code((的file_get_contents($网址)));
的file_put_contents($缓存,json_en code($ jsonData));
} $结果='&LT; DIV ID =Instagram的&GT;'PHP_EOL。
的foreach($ jsonData-&GT;数据作为$关键=&GT; $值){
。$结果=\\ t的'&LT;一类=的fancybox数据的fancybox组=画廊
标题=。ヶ辆($值 - &GT; caption-&gt;文字)。 .htmlentities(日期(缩略词,Y,G:我一个,$值 - &GT; caption-&GT; CREATED_TIME))。'
风格=填充:3PX的href ='$值 - &GT;图像 - &GT; standard_resolution-&GT;网址。'&GT;
&LT; IMG SRC ='$值 - 方式&gt;图像 - &GT; low_resolution-&GT; URL。'ALT ='$值 - 方式&gt; caption-方式&gt;文'。WIDTH ='$宽度。'HEIGHT ='$高度'/&GT;
&所述; /一个方式&gt;'PHP_EOL;
}
$结果='&LT; / DIV&GT;'。PHP_EOL。
返回$结果;
}回声get_instagram();
?&GT;
随着
$值 - &GT;&位置 - GT;名称
如果您要检查其空的,如果是则不要表现出来,但也希望如果它被设置,你会做这样的事情一个字符串追加到它:
$位置=(空($值 - &GT;&位置 - GT;名)!)?'@'.$值 - &GT;&位置 - GT;名:空;
然后使用 $位置
来呼应你想要它。
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屋!