I have an issue I can't figure out how to solve: I load some facebook posts on a website using their api. The posts can have videos, so I want to load those videos in an iframe, ofcourse the correct video must be loaded with the post it belongs to.
Easier said than done, because that information is stored inside two objects, the posts
object contains stuff like the post message etcetera while the videos
object contains all videos from a page.
I want to use information from both objects inside one loop, so my idea was to combine both objects/arrays like so:
<?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);
?>
When I print $merged, it shows one big array with first all posts and then all videos (as expected). How can I make sure, that when I loop this array, the correct embed_html
field is loaded with the correct post? I noticed they are connected somehow with their ids. A post id looks like this for example:
367712570004555_1287302444712225
While a video id looks like:
1287302444712225
So I need to explode the post id on the underscore, if I then take the last part, both ids are the same. Knowing that info, how can I use it to load the correct embed_html with the correct post inside a loop?
To clarify here is part of the posts
object (when not merged ofcourse):
{
"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"
},
And the videos
object:
{
"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"
},
I tried this and hope you're trying to do the same as per your question merging posts and videos as per there ids.
<?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);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.