Skip to content

Commit

Permalink
Configuration for Facebook (facebookAppID and facebookAppSecret) is n…
Browse files Browse the repository at this point in the history
…ow required.
  • Loading branch information
ivopetkov committed Mar 26, 2021
1 parent 3584fe4 commit 70ad83d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
18 changes: 11 additions & 7 deletions src/VideoEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class VideoEmbed
static private $providers = [
'CollegeHumor' => ['collegehumor.com'],
'Dailymotion' => ['dailymotion.com'],
'Facebook' => ['facebook.com'],
'Facebook' => ['facebook.com', 'fb.watch'],
'Flickr' => ['flickr.com', '*.flickr.com', 'flic.kr'],
'FunnyOrDie' => ['funnyordie.com'],
'Hulu' => ['hulu.com'],
Expand All @@ -119,31 +119,36 @@ class VideoEmbed
* Creates a new VideoEmbed object and load it if an url is specified
*
* @param string $url The video url
* @param array $config Configuration options. Currently supported: facebookAppID and facebookAppSecret
*/
public function __construct($url = null)
public function __construct($url = null, $config = [])
{
if ($url !== null) {
$this->load($url);
$this->load($url, $config);
}
}

/**
* Loads the data for the url specified
*
* @param string $url The video url
* @param array $config Configuration options. Currently supported: facebookAppID and facebookAppSecret
* @throws \Exception
* @throws \InvalidArgumentException
* @return void No value is returned
*/
public function load($url)
public function load($url, $config = [])
{
if (!is_string($url)) {
throw new \InvalidArgumentException('The url argument must be of type string');
}
if (!is_array($config)) {
throw new \InvalidArgumentException('The config argument must be of type array');
}
$this->url = $url;

// Converts PHP errors and warnings to Exceptions
set_error_handler(function() {
set_error_handler(function () {
throw new \Exception(func_get_arg(1));
});

Expand All @@ -160,7 +165,7 @@ public function load($url)
foreach ($domains as $domain) {
if (preg_match('/^' . str_replace(['.', '*'], ['\.', '.*'], $domain) . '$/', $hostname)) {
include_once __DIR__ . DIRECTORY_SEPARATOR . 'VideoEmbed' . DIRECTORY_SEPARATOR . 'Internal' . DIRECTORY_SEPARATOR . 'Providers' . DIRECTORY_SEPARATOR . $name . '.php';
call_user_func(['\IvoPetkov\VideoEmbed\Internal\Providers\\' . $name, 'load'], $this->url, $this);
call_user_func(['\IvoPetkov\VideoEmbed\Internal\Providers\\' . $name, 'load'], $this->url, $this, $config);
$done = true;
break;
}
Expand Down Expand Up @@ -205,5 +210,4 @@ public function setSize($width, $height)
$this->width = $width;
$this->height = $height;
}

}
18 changes: 12 additions & 6 deletions src/VideoEmbed/Internal/Providers/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
final class Facebook extends \IvoPetkov\VideoEmbed\Internal\Provider
{

static function load($url, $result)
static function load($url, $result, $config)
{
$response = parent::readUrl('https://www.facebook.com/plugins/video/oembed.json/?url=' . urlencode($url));
if (!isset($config['facebookAppID'])) {
throw new \Exception('The facebookAppID config option is missing!');
}
if (!isset($config['facebookAppSecret'])) {
throw new \Exception('The facebookAppSecret config option is missing!');
}
$response = parent::readUrl('https://graph.facebook.com/v10.0/oembed_video?url=' . urlencode($url) . '&access_token=' . $config['facebookAppID'] . '|' . $config['facebookAppSecret']);
$result->rawResponse = $response;
$data = json_decode($response, true);
if (is_array($data)) {
$urlParts = explode('/', trim($url, '/'));
$videoID = $urlParts[sizeof($urlParts) - 1];
if (is_array($data) && isset($data['html'])) {
$matches = null;
preg_match('/\/videos\/([0-9]+)\//', $data['html'], $matches);
$videoID = isset($matches[1]) ? $matches[1] : null;
if (is_numeric($videoID)) {
$result->width = parent::getIntValueOrNull($data, 'width');
$result->height = parent::getIntValueOrNull($data, 'height');
Expand All @@ -37,5 +44,4 @@ static function load($url, $result)
}
}
}

}
12 changes: 7 additions & 5 deletions tests/providersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public function testDailyMotion()
*/
public function testFacebook()
{
$videoEmbed = new IvoPetkov\VideoEmbed('https://www.facebook.com/facebook/videos/10155479830046729/');
$this->assertTrue($videoEmbed->url === 'https://www.facebook.com/facebook/videos/10155479830046729/');
$this->assertTrue($videoEmbed->title === null);
$videoEmbed->setSize(800, 600);
$this->assertTrue($videoEmbed->html === '<iframe src="https://www.facebook.com/video/embed?video_id=10155479830046729" width="800" height="600" frameborder="0"></iframe>');
// this test requires a configuration (facebookAppID and facebookAppSecret)
$this->assertTrue(true);
// $videoEmbed = new IvoPetkov\VideoEmbed('https://www.facebook.com/facebook/videos/10155479830046729/');
// $this->assertTrue($videoEmbed->url === 'https://www.facebook.com/facebook/videos/10155479830046729/');
// $this->assertTrue($videoEmbed->title === null);
// $videoEmbed->setSize(800, 600);
// $this->assertTrue($videoEmbed->html === '<iframe src="https://www.facebook.com/video/embed?video_id=10155479830046729" width="800" height="600" frameborder="0"></iframe>');
}

/**
Expand Down

0 comments on commit 70ad83d

Please sign in to comment.