Skip to content

Instantly share code, notes, and snippets.

@janzell
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janzell/a6fd3c19db30494f6ef7 to your computer and use it in GitHub Desktop.
Save janzell/a6fd3c19db30494f6ef7 to your computer and use it in GitHub Desktop.
PHP - Facebook Video Getter
<?php
class Facebook{
/**
* Video Codec
* @var string
*/
public $codec = 'h264';
/**
* Video Quality
*
* @var array
*/
public $quality = array('sd', 'hd', 'mobile');
/**
* Class Constructor
*/
public function __construct()
{
//constructor
}
/**
* Get Video
*
* @param string $url
* @return [type]
*/
public function get_video($url ="")
{
$page = $this->get_remote_content($url);
$dom = new DOMDocument("1.0", "utf-8");
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $page);
$xPath = new DOMXpath($dom);
$video = $xPath->query('//div[@class = "player_container"]/div[@class = "player"]')->item(0);
if ($video) {
$json = json_decode($this->get_remote_content($video->getAttribute('data-config-url')));
if (property_exists($json, 'message')) {
return false;
}
$videoObject = null;
if (property_exists($json->request->files, $this->vimeo_video_codec)) {
$videoObject = $this->get_quality_video($json->request->files->{$this->codec});
}
// pre($videoObject);
if ($videoObject) {
return $videoObject;
}
}
return false;
}
/**
* Get Video Information
*
* @param string $url
* @return [type]
*/
public function get_video_info( $url = "")
{
//get the video
$video_id = $this->get_video_id($url);
$graph_url = 'https://graph.facebook.com/';
if(!empty($video_id) ){
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $graph_url . $video_id );
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$output = json_decode($result);
return $output;
// return $output->format;
}
}
/**
* Get vimeo video object
*
* @param stdClass $files object of Vimeo files
* @return stdClass Video file object
*/
private function get_quality_video($files)
{
$video = null;
foreach ($this->quality as $quality) {
if (property_exists($files, $quality)) {
$video[] = $files->{$quality};
// break;
}
}
if (!$video) {
foreach (get_object_vars($files) as $file) {
$video = $file;
break;
}
}
return $video;
}
/**
* Get remote content by URL
*
* @param string $url remote page URL
* @return string result content
*/
private function get_remote_content($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'spider');
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
/**
* Get video id based on URL
*
*
* @param string $url
* @return [type]
*/
public function get_video_id( $url = "")
{
$v = '';
if( !empty($url) ){
$arr = explode('/',$url);
//get the index 3
$id_arr = explode('?', $arr[3]);
//parse the query string
parse_str($id_arr[1]);
}
return $v;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment