2013-01-19 22 views
5
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX'; 
echo json_decode(file_get_contents($url))->{'followed_by'}; 

我正在使用此代碼,我不明白是什麼問題。我是PHP新手,所以請原諒這個新手的錯誤。我試圖讓「followed_by」自己顯示。我設法讓Facebook的「喜歡」和Twitter的追隨者以這種方式展示。使用PHP從Instagram獲取基本信息

回答

2

根據Instagram API Docsfollowed_bycounts的孩子,它是data的孩子。

https://api.instagram.com/v1/users/1574083/?access_token=ACCESS-TOKEN 

返回:

{ 
"data": { 
    "id": "1574083", 
    "username": "snoopdogg", 
    "full_name": "Snoop Dogg", 
    "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg", 
    "bio": "This is my bio", 
    "website": "http://snoopdogg.com", 
    "counts": { 
     "media": 1320, 
     "follows": 420, 
     "followed_by": 3410 
    } 
} 

下因此應工作。

<?php 
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX'; 
$api_response = file_get_contents($url); 
$record = json_decode($api_response); 
echo $record->data->counts->followed_by; 

// if nothing is echoed try 
echo '<pre>' . print_r($api_response, true) . '</pre>'; 
echo '<pre>' . print_r($record, true) . '</pre>'; 
// to see what is in the $api_response and $record object 
+0

我剛剛嘗試過你給出的代碼,但它不起作用(我已經嘗試了兩個echo自己)。我甚至確保我的api.instagram.com/xxx是有效的,它是。 –

+0

@NazarAbubaker - 你確定你的access_token是正確的 - 你是如何生成它的?嘗試上面編輯的代碼,並讓我們知道API實際返回的內容。 – PassKit

+0

我用它來生成access_token [link] http://jelled.com/instagram/access-token [/ link]。從[鏈接] http://stackoverflow.com/questions/12677551/how-to-get-access-token-from-instagram-token-using-jquery-or-php [/鏈接]採取 我得到的是「警告:file_get_contents()[function.file-get-contents]:無法找到包裝" https:" - 您忘了在配置PHP時啓用它嗎?在XXX上第36行」 –

1

試試這個..

<?php 
$instagram = "https://api.instagram.com/v1/users/xxxxx/?access_token=xxxxx"; 
$instagram_follows = json_decode(file_get_contents($instagram))->data->counts->followed_by; 
echo $instagram_follows; 
?> 
0

試試這個...

$url = 'https://api.instagram.com/v1/users/USER_ID?access_token=YOUR_TOKEN'; 
$api_response = file_get_contents($url); 
$record = json_decode($api_response); 

echo $followed_by = $record->data->counts->followed_by; 

Click來獲得用戶的所有信息

1
function get_https_content($url=NULL,$method="GET"){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_VERBOSE, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0'); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    return curl_exec($ch); 
} 

function ig_count($username) { 
    return json_decode(get_https_content("https://api.instagram.com/v1/users/1460891826/?client_id=ea69458ef6a34f13949b99e84d79ccf2"))->data->counts->followed_by; 
} 

這裏是我的代碼: )

14

在你需要抓住跟隨數(或其他字段),不登錄的情況下,Instagram的是不夠好,把他們在JSON頁面的源代碼中:

$raw = file_get_contents('https://www.instagram.com/USERNAME'); //replace with user 
preg_match('/\"followed_by\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m); 
print intval($m[1]); 

//returns "123" 

希望有所幫助。

2016年5月24日更新,以更寬容的JSON空間。

+0

它爲我工作。 –

+0

仍然適用於我 - 2017年3月:-D – michaelmcgurk

+1

經過**小時**嘗試使用Instagram API實現此功能後,我最終使用了此解決方案。 **謝謝**。 最糟糕的API設計和文檔我從來沒有見過。你甚至不得不發送一個_screencast_來解釋你想用API做什麼來擺脫沙箱模式。我只想讓任何給定用戶的追隨者數量,無論如何這是公共信息。這是瘋狂。 – Marc