我有一个问题,我无法弄清楚如何解决:我使用他们的api在网站上加载一些Facebook帖子。帖子可以有视频,所以我想在iframe中加载这些视频,当然正确的视频必须加载它所属的帖子。
说起来容易做起,因为该信息存储在两个对象中,posts
对象包含帖子消息等内容,而videos
对象包含页面中的所有视频。
我想在一个循环中使用来自两个对象的信息,所以我的想法就是像这样组合两个对象/数组:
<?php
$json_object = file_get_contents("https://graph.facebook.com/v2.10/pageid/posts?fields=full_picture,message,picture,story,permalink_url,updated_time,from&access_token=myaccesstoken");
$feedarray = json_decode($json_object);
$json_objectvideo = file_get_contents("https://graph.facebook.com/v2.10/pageid/videos?fields=embed_html,permalink_url&access_token=myaccesstoken");
$feedvideo = json_decode($json_objectvideo);
$merged = array_merge($feedarray->data, $feedvideo->data);
?>
当我打印$ merge时,它显示一个大数组,首先是所有帖子,然后是所有视频(如预期的那样)。我怎样才能确定,当我循环这个数组时,正确的embed_html
字段加载了正确的帖子?我注意到他们与他们的ID有某种联系。帖子ID如下所示:
367712570004555_1287302444712225
视频ID如下所示:
1287302444712225
所以我需要在下划线上爆炸post id,如果我接下来的话,两个id都是相同的。知道了这些信息后,如何使用它在循环内加载正确的帖子加载正确的embed_html?
这里澄清的是posts
对象的一部分(当没有合并时):
{
"data": [
{
"created_time": "2017-09-12T20:58:21+0000",
"message": "Vandaag bij Vivienne Westwood Boutique Amsterdam de lichtreclame gemonteerd. Mooi project om te mogen doen.\n\u2705 maatwerk\n\u2705 kwaliteit\n\u2705 betaalbaar\n\ud83d\udc4b Benieuwd naar de kosten? We helpen je graag!\n- Reclame & Internet - #ontwerp > #productie > #montage",
"id": "367712570004555_1287302444712225"
},
videos
对象:
{
"data": [
{
"embed_html": "\u003Ciframe src=\"https://www.facebook.com/plugins/video.php?href=https\u00253A\u00252F\u00252Fwww.facebook.com\u00252Fwebsite\u00252Fvideos\u00252F1287302444712225\u00252F&width=1280\" width=\"1280\" height=\"720\" style=\"border:none;overflow:hidden\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" allowFullScreen=\"true\">\u003C/iframe>",
"permalink_url": "/website/videos/1287302444712225/",
"id": "1287302444712225"
},
答案 0 :(得分:0)
我尝试了这一点,并希望您尝试根据您的问题执行相同的操作,根据这些ID合并帖子和视频。
<?php
$posts = '{
"data": [
{
"created_time": "2017-09-12T20:58:21+0000",
"message": "Vandaag bij Vivienne Westwood Boutique Amsterdam de lichtreclame gemonteerd. Mooi project om te mogen doen.\n\u2705 maatwerk\n\u2705 kwaliteit\n\u2705 betaalbaar\n\ud83d\udc4b Benieuwd naar de kosten? We helpen je graag!\n- Reclame & Internet - #ontwerp > #productie > #montage",
"id": "367712570004555_1287302444712225"
}
]
}';
$videos = '{
"data": [
{
"embed_html": "\u003Ciframe src=\"https://www.facebook.com/plugins/video.php?href=https\u00253A\u00252F\u00252Fwww.facebook.com\u00252Fwebsite\u00252Fvideos\u00252F1287302444712225\u00252F&width=1280\" width=\"1280\" height=\"720\" style=\"border:none;overflow:hidden\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" allowFullScreen=\"true\">\u003C/iframe>",
"permalink_url": "/website/videos/1287302444712225/",
"id": "1287302444712225"
}
]
}';
$x = json_decode($posts, true)['data'];
$y = json_decode($videos, true)['data'];
foreach($x as $key => $value) {
$id = explode('_', $value['id'])[1];
foreach($y as $key => $vide){
if($vide['id'] == $id) {
$combinedArr[] = array_merge($vide, $value);
}
}
}
print_r($combinedArr);
?>
希望这有帮助!