posast
posast

Reputation: 65

How get a video id from given Facebook URL?

Any ideas how could I get facebook id of video. First I had this, but it doesn't work always because sometime there is something aftern id. For example. $url = https://www.facebook.com/photo.php?v=747864965250852&set=vb.530843830286301&type=2&theater.

$suburl = substr("$url", -15);

Now I have this, and it works better, but still sometime someone doesn't add https or something like that.

$suburl = substr ("$url", 37);

So any ideas how to get just id (15 characters). You must consider that link won't always be the same. The best way would be that I would have substr 15 character after v=.

Upvotes: 2

Views: 2352

Answers (2)

Phong Hunterist
Phong Hunterist

Reputation: 1

That's the better way:

/*
* case1: https://www.facebook.com/video.php?v=10153231379946729
* case2: https://www.facebook.com/facebook/videos/10153231379946729
* case3: https://www.facebook.com/facebook/posts/10153231379946729
*/
/* case 1 */
$parts = parse_url($url);
if(isset($parts['query'])){
    parse_str($parts['query'], $qs);
    if(isset($qs['v'])){
        return $qs['v'];
    }
}
/*case 2 and 3*/
if(isset($parts['path'])){
    $path = explode('/', trim($parts['path'], '/'));
    return $path[count($path)-1];
}

Upvotes: 0

spinsch
spinsch

Reputation: 1415

use parse_url to split the parts from url:

<?php

$url = "https://www.facebook.com/photo.php?v=747864965250852&set=vb.530843830286301&type=2&theater";
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);

var_dump($vars);

output

array (size=4)
  'v' => string '747864965250852' (length=15)
  'set' => string 'vb.530843830286301' (length=18)
  'type' => string '2' (length=1)
  'theater' => string '' (length=0)

More about this functions

Upvotes: 2

Related Questions