// Gets our data
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;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/tags/{$tagname}/media/recent?client_id={$userid}&access_token={$accessToken}");
$result = json_decode($result);
$result1 = fetchData("{$result->pagination->next_url}");
$result1 = json_decode($result1);
如何将两个结果合并到一个数组中? 我想制作一个循环,所以我从标签中获取所有图片,所以我可以事后对它进行排序,因为我使用图片网址,我该怎么做?
答案 0 :(得分:0)
试试这个:
$result_array = array_merge($result, $result1);
答案 1 :(得分:0)
array_merge()应该为现有代码提供技巧。
如果您正在寻求使用似乎维护得很好的框架来实现解决方案,请查看使用InstaPHP框架(http://instaphp.com)。我最近用它来结婚的一对夫妇的网站。这很容易实现。您需要注册才能使用Instagram Developer API(http://instagram.com/developer/register/),但我想您可能已经这样做了。
修改强>
InstaPHP v1的简化使用(我的项目不久前)如下所示:
<?php
include_once 'instaphp/instaphp.php';
function getInstragramPhotos($hash_tag) {
//-- Get an instance of the Instaphp object
$api = Instaphp\Instaphp::Instance();
//-- Get the response for Popular media
$response = $api->Tags->Recent($hash_tag);
//-- Check if an error was returned from the API
if (empty($response->error)) {
foreach ($response->data as $item) {
printf('<img class="instagram" src="%s" width="%d" height="%d" alt="%s">', $item->images->thumbnail->url, $item->images->thumbnail->width, $item->images->thumbnail->height, empty($item->caption->text) ? 'Untitled':$item->caption->text);
}
}
}
getInstragramPhotos('YourHashTag');
?>
然后在instaphp文件夹的根目录下的config.xml文件中:
<?xml version="1.0" encoding="utf-8" ?>
<Config>
<Instagram>
<Endpoint timeout="10">https://api.instagram.com</Endpoint>
<Version>v1</Version>
<ClientId>YOURCLIENTIDFORINSTAGRAM</ClientId>
<ClientSecret>YOURCLIENTSECRETFORINSTAGRAM</ClientSecret>
<Scope>comments+likes+relationships</Scope>
<OAuthPath>/oauth/authorize/?client_id={ClientId}&response_type=code&redirect_uri={RedirectUri}</OAuthPath>
<OAuthTokenPath>/oauth/access_token/</OAuthTokenPath>
</Instagram>
<Instaphp>
<RedirectUri></RedirectUri>
<CACertBundlePath></CACertBundlePath>
<Cache Engine="File" Enabled="true">
<Settings>
<Setting Path="tmp/cache"/>
<Setting TTL="+2 Minutes"/>
</Settings>
</Cache>
</Instaphp>
</Config>
答案 2 :(得分:0)
您需要将true
标记添加到json_decode()
,以便它返回数组而不是对象,然后您需要使用array_merge()
E.g。
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);
$mergedArray = array_merge($array1, $array2);