Skip to content

Commit

Permalink
Add support for Instagram.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Sep 8, 2022
1 parent 847d435 commit ba7f1fb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/VideoEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class VideoEmbed
//'CollegeHumor' => ['collegehumor.com'],
'Dailymotion' => ['dailymotion.com'],
'Facebook' => ['facebook.com', 'fb.watch'],
'Instagram' => ['instagram.com'],
'Flickr' => ['flickr.com', '*.flickr.com', 'flic.kr'],
//'FunnyOrDie' => ['funnyordie.com'],
'Hulu' => ['hulu.com'],
Expand Down
43 changes: 43 additions & 0 deletions src/VideoEmbed/Internal/Providers/Instagram.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Video embed
* https://github.com/ivopetkov/video-embed
* Copyright (c) Ivo Petkov
* Free to use under the MIT license.
*/

namespace IvoPetkov\VideoEmbed\Internal\Providers;

final class Instagram extends \IvoPetkov\VideoEmbed\Internal\Provider
{

static function load($url, $result, $config)
{
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/instagram_oembed?url=' . urlencode($url) . '&access_token=' . $config['facebookAppID'] . '|' . $config['facebookAppSecret']);
$result->rawResponse = $response;
$data = json_decode($response, true);
if (is_array($data) && isset($data['html'])) {
$matches = null;
preg_match('/\/reel\/([0-9a-zA-Z\-\_]+)\//', $data['html'], $matches);
$videoID = isset($matches[1]) ? $matches[1] : null;
if ($videoID !== null) {
$result->width = parent::getIntValueOrNull($data, 'width');
$result->height = parent::getIntValueOrNull($data, 'width');
$result->html = '<iframe src="https://www.instagram.com/reel/' . $videoID . '/embed/captioned" width="' . $result->width . '" height="' . $result->height . '" frameborder="0"></iframe>';
$result->thumbnail['url'] = parent::getStringValueOrNull($data, 'thumbnail_url');
$result->thumbnail['width'] = parent::getIntValueOrNull($data, 'thumbnail_width');
$result->thumbnail['height'] = parent::getIntValueOrNull($data, 'thumbnail_height');
$result->author['name'] = parent::getStringValueOrNull($data, 'author_name');
$result->provider['name'] = parent::getStringValueOrNull($data, 'provider_name');
$result->provider['url'] = parent::getStringValueOrNull($data, 'provider_url');
}
}
}
}
9 changes: 9 additions & 0 deletions tests/providersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public function testFacebook()
// $this->assertTrue($videoEmbed->html === '<iframe src="https://www.facebook.com/video/embed?video_id=10155479830046729" width="800" height="600" frameborder="0"></iframe>');
}

/**
*
*/
public function testInstagram()
{
// this test requires a configuration (facebookAppID and facebookAppSecret)
$this->assertTrue(true);
}

/**
*
*/
Expand Down

0 comments on commit ba7f1fb

Please sign in to comment.