2011-06-10 68 views
15

我想用PHP獲取用戶的Instagram飼料。我已經註冊了一個Instagram開發者帳戶,並嘗試拉入用戶的信息和照片,但回覆不穩定。有時候我會收到回覆,其他時間我會收到錯誤:access_token丟失。有沒有一個通過用戶名獲取用戶的照片源的可靠示例?如何獲得用戶的Instagram飼料

理想情況下,我想它是那樣簡單:

$instagram = new Instagram(); 
$photos = $instagram->getPhotos("username-goes-here"); 

如果Instagram的是處理所有請求的類。任何幫助或方向表示讚賞。謝謝!

+0

Instagram的API改變很多和其他答案不再工作我發現以下一步一步的教程非常有用檢索Instagram的飼料https://www.codeofaninja.com/2015/01/display-instagram-feed-website.html – Kris 2016-07-15 08:05:55

回答

53

試試這個,

<?php 

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

    $result = fetchData("https://api.instagram.com/v1/users/ID-GOES-HERE/media/recent/?access_token=TOKEN-GOES-HERE"); 
    $result = json_decode($result); 
    foreach ($result->data as $post) { 
    // Do something with this data. 
    } 
?> 

願這幫助你。

+4

這應該被標記爲接受的答案@ jaysonp – timbillstrom 2013-12-18 14:00:31

+4

這是非常好的。也可能有助於一些:獲取OAuth令牌:http://jelled.com/instagram/access-token獲取用戶名ID:http://jelled.com/instagram/lookup-user-id – deweydb 2014-10-17 22:15:02

+0

它適用於我,但只適用於生產環境。出於某種原因,本地主機請求是空的。 – 2016-02-09 17:29:07

6

我這樣做:

<?php 

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

    $result = fetchData("https://api.instagram.com/v1/users/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14"); 


    $result = json_decode($result); 
    foreach ($result->data as $post) { 
    if(empty($post->caption->text)) { 
     // Do Nothing 
    } 
    else { 
     echo '<a class="instagram-unit" target="blank" href="'.$post->link.'"> 
     <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" /> 
     <div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>'; 
    } 

    } 
?> 
4

以什麼我已經在互聯網與此頁面上看到的,我已經創建了一個Instagram的類(很簡單,只拉飼料等')以下。

class Instagram { 
    public static $result; 
    public static $display_size = 'thumbnail'; // you can choose between "low_resolution", "thumbnail" and "standard_resolution" 
    public static $access_token = "DEFAULTACCESSTOKEN"; // default access token, optional 
    public static $count = 10; 
    public static function fetch($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; 
    } 
    function __construct($Token=null){ 
     if(!empty($Token)){ 
      self::$access_token = $Token; 

      // Remove from memory -- not sure if really needed. 
      $Token = null; 
      unset($Token); 
     } 
     self::$result = json_decode(self::fetch("https://api.instagram.com/v1/users/self/media/recent?count=" . self::$count . "&access_token=" . self::$access_token), true); 
    } 
} 
$Instagram = new Instagram('ACCESSTOKENIFCHANGEDORNULLOREMPTY'); 
foreach ($Instagram::$result->data as $photo) { 
    $img = $photo->images->{$Instagram::$display_size}; 
} 
1

更新:2017年6月15日 - Instagram的改變了終點,下列不工作了。

因爲它是不再可能獲得隨機用戶喂不批准申請,我已經想通了如何使用非官方API獲得它:

#!/bin/bash 
instagram_user_id=25025320 
count=12 
csrftoken=$(curl --head -k https://www.instagram.com/ 2>&1 | grep -Po "^Set-Cookie: csrftoken=\K(.*?)(?=;)") 
curl "https://www.instagram.com/query/" -H "cookie: csrftoken=$csrftoken;" -H "x-csrftoken: $csrftoken" -H "referer: https://www.instagram.com/" --data "q=ig_user($instagram_user_id)%20%7B%20media.after(0%2C%20$count)%20%7B%0A%20%20count%2C%0A%20%20nodes%20%7B%0A%20%20%20%20caption%2C%0A%20%20%20%20code%2C%0A%20%20%20%20comments%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20date%2C%0A%20%20%20%20dimensions%20%7B%0A%20%20%20%20%20%20height%2C%0A%20%20%20%20%20%20width%0A%20%20%20%20%7D%2C%0A%20%20%20%20display_src%2C%0A%20%20%20%20id%2C%0A%20%20%20%20is_video%2C%0A%20%20%20%20likes%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20owner%20%7B%0A%20%20%20%20%20%20id%2C%0A%20%20%20%20%20%20username%2C%0A%20%20%20%20%20%20full_name%2C%0A%20%20%20%20%20%20profile_pic_url%0A%20%20%20%20%7D%2C%0A%20%20%20%20thumbnail_src%2C%0A%20%20%20%20video_views%0A%20%20%7D%2C%0A%20%20page_info%0A%7D%0A%20%7D" -k 

稍後我會改進這個答案用PHP,我也需要用PHP來做到這一點。

-1

試試這一個原始形式的履帶式。

function feed_instagram($url = "https://www.instagram.com/titaniumheart_") 
{ 
    //$url ie https://www.instagram.com/titaniumheart_ 

    $dom = new DOMDocument(); 
    @$dom->loadHTMLFile($url); 
    $f=$dom->saveHTML(); //load the url (crawl) 

    $key="";  
    $swquote=0;  
    echo "<div>"; 

    for ($x=0;$x<strlen($f);$x++) 
    { 
     $c=substr($f,$x,1); 
     //echo $c."-"; 
     if ($c==chr(34)) 
     { 
      if($swquote==0) 
      { 
       $swquote=1; //to start get chars 
      } else 
      { 
       $swquote=0; 
       //echo $key; 
       if($key=="code") 
       { 
        //get the number of comments 
        $m=substr($f,$x+4,100); 
        $code= substr($m,0,strpos($m,chr(34))); 
        echo "code is ".$code; 
        echo "<br>"; 
       }    
       if($key=="comments") 
       { 
        //get the number of comments 
        $m=substr($f,$x+12,20); 
        $comments= substr($m,0,strpos($m,"}")); 
        echo "number of comments is ".$comments; 
        echo "<br>"; 
       } 
       if($key=="caption") 
       { 
        //get the number of comments 
        $m=substr($f,$x+4,200); 
        $caption= substr($m,0,strpos($m,chr(34))); 
        echo "caption is ".$caption; 
        echo "<br>"; 
       } 
       if($key=="likes") 
       { 
        //get the number of comments 
        $m=substr($f,$x+12,20); 
        $likes= substr($m,0,strpos($m,"}")); 
        echo "number of likes is ".$likes; 
        echo "<br>"; 
       } 
       if($key=="thumbnail_src") 
       { 
        //get the number of comments 
        $m=substr($f,$x+4,200); 
        $src= substr($m,0,strpos($m,"?")); 
        echo "<br>image source is ".$src; 
        echo "<br>";          
        echo "<a href=\"https://www.instagram.com/p/".$code."/\">"; 
        echo "<img src=\"".$src."\">"; 
        echo "</a><br>";      
       }        
       $key=""; 
     } 

    }else 
    { 
     if($swquote==1) 
     { 
      $key.=$c; 
     } 
    }   
} 
echo "</div>"; 
} 

用法:https://www.instagram.com/titaniumheart_「);>

請注意:?您必須啓用擴展。 」在php.ini中php_openssl「

+0

不像用戶所要求的那樣使用Instagram API,這是加載前端HTML和遍歷DOM。 – 2016-09-21 11:06:29