2016-04-10 53 views
0

我想從JSON網址中提取任何instagram用戶的profile_picture:https://www.instagram.com/just/media/用PHP打印出來。我已經試過:Instagram JSON頭像提取PHP

$instaJSONUrl = 'https://www.instagram.com/just/media/'; 
$json = file_get_contents($instaJSONUrl); 
$json_data = json_decode($json, true); 
echo $json_data["profile_picture"]; 

不工作。如果我打印出$ JSON而已,我得到:

{status":"ok","items":[],"more_available":false} 

也就意味着問題是讀取URL。這是否意味着我需要訪問令牌,因爲我不想使用它,因爲當我從瀏覽器打開https://www.instagram.com/just/media/時,我已經可以獲取配置文件圖像URL。

回答

1

這將產生一組用戶及其圖片配置文件鏈接。

<?php 

$instaJSONUrl = 'https://www.instagram.com/just/media/'; 
$json = file_get_contents($instaJSONUrl); 
$json_data = json_decode($json, true); 

$users = array(); 

foreach($json_data['items'] as $item) 
    foreach($item['comments'] as $comment) 
      foreach($comment as $profile) 
        $users[$profile['from']['username']] = $profile['from']['profile_picture']; 


print_r($users); 
+0

問題是,它無法訪問整個JSON,就像它通過瀏覽器打開時一樣。基本上它讀爲:{狀態「:」確定「,」項目「:[],」more_available「:false},所以它沒有任何項目。檢查出我得到的解決方案:-) –

+0

啊,很高興你有它的工作 –

0

成功地通過直接訪問meta標籤做到這一點,並專門<meta property="og:image"標籤,有如下:

libxml_use_internal_errors(true); 
$c = file_get_contents($instaURL); 
$d = new DomDocument(); 
$d->loadHTML($c); 
$xp = new domxpath($d); 

,然後我可以打印出與圖像網址:

foreach ($xp->query("//meta[@property='og:image']") as $el) { 
    echo $el->getAttribute("content"); 
} 


您甚至可以使用它來訪問其他屬性,如og:urlog:description爲特定的Instagram用戶。

這是所有那些需要訪問的人&獲取Instagram用戶數據(基本:profile_image,profile_url,profile_description等),而不涉及Instagram的API。