簡體   English   中英

從Instagram標簽獲取所有圖片

[英]Get all pictures from a Instagram tag

// 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);   

如何將兩個結果合並為一個數組? 我想進行循環,這樣我就可以從主題標簽中獲取所有圖片,因此以后可以對其進行排序,因為我使用的是圖片網址,我該怎么辦?

嘗試這個 :

$result_array = array_merge($result, $result1);

參考: http : //php.net/manual/zh/function.array-merge.php

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}&amp;response_type=code&amp;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>

您需要將true標志添加到json_decode()以便它返回數組而不是對象,然后您需要使用array_merge()

例如

$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);
$mergedArray = array_merge($array1, $array2);

請參閱: http//php.net/manual/en/function.json-decode.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM