我在PHP中设置了4个API调用,以返回Facebook,Twitter,Instagram和YouTube帐户的供稿。全部以JSON格式返回。
我要做的是:
这是我目前的代码(删除了API密钥/用户名):
<?php
date_default_timezone_set('Europe/London');
require_once('TwitterAPIExchange.php');
//
// GET FACEBOOK
//
$API_KEY = 'XXXXX';
$API_SECRET = 'XXXXXX';
$URL = 'https://graph.facebook.com/v2.1';
$token = file_get_contents($URL . '/oauth/access_token?'
. 'client_id=' . $API_KEY
. '&client_secret=' . $API_SECRET
. '&grant_type=client_credentials');
$data = file_get_contents($URL . '/XXXXXX/posts?' . $token . '&format=json&method=get&pretty=0&suppress_http_code=1');
$json = json_decode($data, true);
// echo "<pre>";
// print_r($json);
// echo "</pre>";
foreach ($json['data'] as $item) {
$fb_feed[] = array(
"source" => "facebook",
"date" => isset($item['created_time']) ? strtotime($item['created_time']) : NULL,
"from" => isset($item['from']['name']) ? $item['from']['name'] : NULL,
"message" => isset($item['message']) ? $item['message'] : NULL,
"shares" => isset($item['shares']['count']) ? $item['shares']['count'] : NULL,
"picture" => isset($item['picture']) ? $item['picture'] : NULL,
);
}
//
// GET INSTAGRAM
//
$insta = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token=XXXXXX', true);
$json = json_decode($insta, true);
foreach ($json['data'] as $item) {
$in_feed[] = array(
"source" => "instagram",
"date" => isset($item['created_time']) ? $item['created_time'] : NULL,
"main_text" => isset($item['caption']['text']) ? $item['caption']['text'] : NULL,
"large_image" => isset($item['images']['standard_resolution']['url']) ? $item['images']['standard_resolution']['url'] : NULL,
"thumbnail" => isset($item['images']['thumbnail']['url']) ? $item['images']['thumbnail']['url'] : NULL,
"link" => isset($item['link']) ? $item['link'] : NULL,
"likes" => isset($item['comments']['count']) ? $item['likes']['count'] : NULL,
"comments" => isset($item['comments']['count']) ? $item['comments']['count'] : NULL,
"video" => isset($item['videos']['standard_resolution']['url']) ? $item['videos']['standard_resolution']['url'] : NULL,
"type" => isset($item['videos']) ? "video" : "photo",
);
}
// echo "<pre>";
// print_r($json);
// echo "</pre>";
//
// GET YOUTUBE
//
$api_key = "XXXXXX";
$channel_id = "XXXXXX";
$yt = 'https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails&key=' . $api_key . '&id=' . $channel_id;
$yt_data = json_decode(file_get_contents($yt), true);
$playlist_id = $yt_data['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
$yt = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&key=' . $api_key . '&playlistId=' . $playlist_id;
$yt_data = json_decode(file_get_contents($yt), true);
$yt_feed = array();
foreach ($yt_data['items'] as $item) {
$yt_feed[] = array(
"source" => "youtube",
"date" => isset($item['snippet']['publishedAt']) ? strtotime($item['snippet']['publishedAt']) : '',
"id" => isset($item['id']) ? $item['id'] : '',
"video_id" => isset($item['contentDetails']['videoId']) ? $item['contentDetails']['videoId'] : '',
"title" => isset($item['snippet']['title']) ? $item['snippet']['title'] : '',
"description" => isset($item['snippet']['description']) ? $item['snippet']['description'] : '',
"thumbnail_standard" => isset($item['snippet']['thumbnails']['standard']['url']) ? $item['snippet']['thumbnails']['standard']['url'] : '',
"thumbnail_high" => isset($item['snippet']['thumbnails']['high']['url']) ? $item['snippet']['thumbnails']['high']['url'] : '',
);
}
//
// GET TWITTER
//
$settings = array(
'oauth_access_token' => "XXXXXX",
'oauth_access_token_secret' => "XXXXXX",
'consumer_key' => "XXXXXX",
'consumer_secret' => "XXXXXX",
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXXXXX';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$user_timeline = $twitter
->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$user_timeline = json_decode($user_timeline, true);
// echo "<pre>";
// print_r($user_timeline);
// echo "</pre>";
$tw_feed = array();
foreach ($user_timeline as $item) {
$tw_feed[] = array(
"source" => "twitter",
"id" => isset($item['id']) ? $item['id'] : '',
"date" => isset($item['created_at']) ? strtotime($item['created_at']) : '',
"text" => isset($item['text']) ? $item['text'] : '',
);
}
$full_array = array_merge($fb_feed, $in_feed, $yt_feed, $tw_feed);
foreach ($full_array as $item) {
if ($item['source'] === "facebook") {
echo "<p>";
echo "Source: " . $item['source'] . "<br>";
echo "Date: " . $item['date'] . "<br>";
echo "From: " . $item['from'] . "<br>";
echo "Message: " . $item['message'] . "<br>";
echo "Shares: " . $item['shares'] . "<br>";
echo "Picture: " . $item['picture'] . "<br>";
echo "</p>";
} elseif ($item['source'] === "instagram") {
echo "<p>";
echo "Source: " . $item['source'] . "<br>";
echo "Date: " . $item['date'] . "<br>";
echo "Main Text: " . $item['main_text'] . "<br>";
echo "Large Image: " . $item['large_image'] . "<br>";
echo "Thumbnail: " . $item['thumbnail'] . "<br>";
echo "Link: " . $item['link'] . "<br>";
echo "Likes: " . $item['likes'] . "<br>";
echo "Comments: " . $item['comments'] . "<br>";
echo "Video: " . $item['video'] . "<br>";
echo "Type: " . $item['type'] . "<br>";
echo "</p>";
} elseif ($item['source'] === "youtube") {
echo "<p>";
echo "Source: " . $item['source'] . "<br>";
echo "Date: " . $item['date'] . "<br>";
echo "ID: " . $item['id'] . "<br>";
echo "Video ID: " . $item['video_id'] . "<br>";
echo "Title: " . $item['title'] . "<br>";
echo "Description: " . $item['description'] . "<br>";
echo "Thumbnail (Standard): " . $item['thumbnail_standard'] . "<br>";
echo "Thumbnail (High): " . $item['thumbnail_high'] . "<br>";
echo "</p>";
} elseif ($item['source'] === "twitter") {
echo "<p>";
echo "Source: " . $item['source'] . "<br>";
echo "Date: " . $item['date'] . "<br>";
echo "Id: " . $item['id'] . "<br>";
echo "Text: " . $item['text'] . "<br>";
echo "</p>";
}
}
?>
代码遍历每个JSON对象并提取所有必需的数据并将其添加到数组(yt_feed,in_feed等),然后使用array_merge组合提要。
关于如何使其按预期工作的任何建议都会很棒..我仍然在学习PHP / jQuery但是我有足够的信心去理解它,只需要朝着正确的方向指出一点。
提前致谢!