Last active
January 26, 2017 08:05
-
-
Save YuzuruSano/8d6932280a3ee9d29447 to your computer and use it in GitHub Desktop.
facebook API 2.8でfacebookページのfeedを取得する(ajaxでのリクエスト・レスポンスを想定)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(isset($_GET['num']) && $_GET['num']){ | |
$num = (int)htmlspecialchars($_GET['num']); | |
}else{ | |
$num = 10; | |
} | |
// アクセストークン | |
define("FACEBOOK_ACCESS_TOKEN", ACCESS TOKEN); | |
// ユーザアカウント名 | |
$user_account = ACCOUNT; | |
//データを取得 | |
$feeds_api_url = 'https://graph.facebook.com/'.$user_account.'/feed?access_token='.FACEBOOK_ACCESS_TOKEN.'&fields=full_picture,id,link,message&limit='.$num; | |
$feeds_data = json_decode(@file_get_contents($feeds_api_url)); | |
$data = array(); | |
foreach ($feeds_data->data as $feed) { | |
$feeddata = array(); | |
//画像 | |
$feeddata['src'] = $feed->full_picture; | |
//パーマリンク | |
$ids = explode('_', $feed->id); | |
$feeddata['link'] = 'http://www.facebook.com/'.$ids[0].'/posts/'.$ids[1]; | |
//テキスト | |
if(property_exists($feed,'message')){ | |
$text = mb_substr($feed->message,0,40,'UTF-8'); | |
//$text = mb_convert_encoding($text, "utf8", "auto"); | |
//$text = mb_convert_encoding($text, "utf8", "utf8"); | |
$feeddata['text'] = preg_replace("/((?:https?|ftp):\/\/[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]+)/u",'<a href="$1">$1</a>',$text); | |
}else{ | |
$feeddata['text'] = ''; | |
} | |
//日付 | |
$ts = $feed->created_time; | |
$date = new DateTime($ts); | |
$feeddata['date'] = $date->format('Y/m/d'); | |
$data[] = $feeddata; | |
} | |
echo json_encode($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment