我正在使用PHP製作Facebook應用程序。通過Facebook API,應用程序顯示登錄用戶的近期帖子。當談到檢索每個帖子的created_time
時,我發現它沒有顯示任何內容或僅顯示1970-01-01T00:00:00+0000
。 (這可以在下面的圖片中看到。)即使當我使用DateTime
時,也是如此。Facebook API PHP - Posts'created_time'數據顯示不正確
它應該返回類似2016-01-31T16:31:26+0000
的東西,如使用Facebook的Graph API Explorer所示。
不知道如何解決這個問題,我能得到它的正確的格式,像這樣顯示:
$ts = strtotime($key['created_time']);
$myTime = gmdate(DATE_ISO8601, $ts);
所有的信息是從Facebook的拉動,是一個foreach
循環中。它通過每一個職位且知道放哪裏,每個created_time
值吃飽爲$key['created_time']
這是我的代碼:
<?php
// Getting all posts published by user
try {
$posts_request = $fb->get('/me/posts?fields=id,created_time,message,link,picture,name&limit=7');
$posts_response = $posts_request->getGraphEdge();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if ($fb->next($posts_response)) {
$total_posts = $posts_response->asArray();
$totalPosts = count($total_posts);
// Form - Post
echo "<p><strong>* With this text box below, you can post to your Facebook Profile about your experience using this application if you wish. Once submitted, this post will then also appear in the list of your other posts below.</strong></p>";
echo "<form action='posttouserstimeline.php' method='post'>";
echo "<textarea name='description' class='posts'></textarea>";
echo "<br><br>";
echo "<input type='submit' class='webbutton' value='Post to Facebook'>";
echo "</form>";
echo "<br>";
echo "<p><strong>* Below are your '5' most resent posts, from your Facebook Profile</strong></p>";
//$date = new DateTime($key['created_time']);
foreach($total_posts as $key) {
$ts = strtotime($key['created_time']);
$myTime = gmdate(DATE_ISO8601, $ts);
echo "Time and Date of Post: ".$myTime."<br>";
echo "<img class='postsprofile' alt='profilepic' src='".$picture['url']."'/> <a href='https://facebook.com/".$profile['id']."' target='_blank'>".$profile['name']."</a><br>";
echo $key['message'].'<br><br>';
echo "<a href='".$key['link']."' target='_blank'><img class='postsprofile' alt='".$key['name']."' src='".$key['picture']."'/></a><br><hr><br>";
//echo "<img class='postsprofile' alt='".$key['name']."' src='".$key['picture']."'/><br>";
//echo "<a href='".$key['link']."' target='_blank'>".$key['link']."</a><br><hr><br>";
}
}