コード例 #1
0
ファイル: link.class.php プロジェクト: haisay/phpfox
 public function getLink($sUrl)
 {
     if (substr($sUrl, 0, 7) != 'http://' && substr($sUrl, 0, 8) != 'https://') {
         $sUrl = 'http://' . $sUrl;
     }
     $aParts = parse_url($sUrl);
     if (!isset($aParts['host'])) {
         return Phpfox_Error::set(Phpfox::getPhrase('link.not_a_valid_link'), true);
     }
     /*
     $key = '';
     $aReturn = array();		
     $oVideo = json_decode(Phpfox_Request::instance()->send('http://api.embed.ly/1/oembed?key=' . $key . '&format=json&maxwidth=400&url=' . urlencode($sUrl), array(), 'GET', $_SERVER['HTTP_USER_AGENT']));
     if (isset($oVideo->error_code)) {
     	throw new \Exception($oVideo->error_message);
     }
     
     if(!isset($oVideo->thumbnail_url))
     {
     	if(preg_match('/facebook/i', $sUrl))
     	{
     		$aRegexes = array(
     			'/https?:\/\/www\.facebook\.com\/v\/([0-9]+)/i', // Flash Embed
     			'/https?:\/\/www\.facebook\.com\/.*\?v=([0-9]+)/i', // New Flash Embed
     			'/https?:\/\/www\.facebook\.com\/video\/embed\?video_id=([0-9]+)/i' // iFrame Embed
     		);
     		
     		foreach($aRegexes as $sRegex)
     		{
     			if (preg_match($sRegex, $sUrl, $aMatches)) 
     			{
     				$iId = $aMatches[1];
     			}
     		}
     		
     		$oThumbnail = json_decode(Phpfox_Request::instance()->send('https://graph.facebook.com/' . $iId . '/picture?redirect=false', array(), 'GET', $_SERVER['HTTP_USER_AGENT']));
     		$oVideo->thumbnail_url = $oThumbnail->data->url;
     		$oVideo->type = 'video';
     		$oVideo->html = '<iframe src="https://www.facebook.com/video/embed?video_id=' . $iId . '" width="400" height="300" frameborder="0"></iframe>';
     	}
     }
     
     if (isset($oVideo->provider_url) || (isset($oVideo->photo)))
     {
     	$aReturn = array(
     		'link' => $sUrl,
     		'title' => (isset($oVideo->title) ? strip_tags($oVideo->title) : ''),
     		'description' => (isset($oVideo->description) ? strip_tags($oVideo->description) : ''),
     		'default_image' => ($oVideo->type == 'photo' ? $oVideo->url : (isset($oVideo->thumbnail_url) ? $oVideo->thumbnail_url : '')),
     		'embed_code' => ($oVideo->type == 'video' ? $oVideo->html : '')
     	);
     			
     	return $aReturn;
     }	
     */
     $aParseBuild = array();
     $sContent = Phpfox_Request::instance()->send($sUrl, array(), 'GET', $_SERVER['HTTP_USER_AGENT'], null, true);
     preg_match_all('/<(meta|link)(.*?)>/i', $sContent, $aRegMatches);
     if (preg_match('/<title>(.*?)<\\/title>/is', $sContent, $aMatches)) {
         $aParseBuild['title'] = $aMatches[1];
     }
     if (isset($aRegMatches[2])) {
         foreach ($aRegMatches as $iKey => $aMatch) {
             if ($iKey !== 2) {
                 continue;
             }
             foreach ($aMatch as $sLine) {
                 $sLine = rtrim($sLine, '/');
                 $sLine = trim($sLine);
                 preg_match('/(property|name|rel|image_src)=("|\')(.*?)("|\')/is', $sLine, $aType);
                 if (count($aType) && isset($aType[3])) {
                     $sType = $aType[3];
                     preg_match('/(content|type)=("|\')(.*?)("|\')/i', $sLine, $aValue);
                     if (count($aValue) && isset($aValue[3])) {
                         if ($sType == 'alternate') {
                             $sType = $aValue[3];
                             preg_match('/href=("|\')(.*?)("|\')/i', $sLine, $aHref);
                             if (isset($aHref[2])) {
                                 $aValue[3] = $aHref[2];
                             }
                         }
                         $aParseBuild[$sType] = $aValue[3];
                     }
                 }
             }
         }
     }
     $image = '';
     $embed = '';
     $MediaEmbed = new MediaEmbed();
     $MediaObject = $MediaEmbed->parseUrl($sUrl);
     if (!$MediaObject instanceof \MediaEmbed\Object\MediaObject) {
         // $xml = simplexml_load_string($sContent, 'SimpleXMLElement', LIBXML_NOERROR |  LIBXML_ERR_NONE);
         // throw new Exception('Does not look like a URL we can embed.');
         if (isset($aParseBuild['og:image'])) {
             $image = $aParseBuild['og:image'];
         }
     } else {
         $image = $MediaObject->image();
         $embed = $MediaObject->getEmbedCode();
     }
     if (isset($aParseBuild['title'])) {
         $aParseBuild['og:title'] = $aParseBuild['title'];
         $aParseBuild['og:description'] = $aParseBuild['description'];
     }
     if (!$image && isset($aParseBuild['og:image'])) {
         $image = $aParseBuild['og:image'];
     }
     return ['link' => $sUrl, 'title' => isset($aParseBuild['og:title']) ? $aParseBuild['og:title'] : '', 'description' => isset($aParseBuild['og:description']) ? $aParseBuild['og:description'] : '', 'default_image' => $image, 'embed_code' => $embed];
 }
コード例 #2
0
ファイル: MediaEmbedTest.php プロジェクト: Lovinity/EQM
 /**
  * Test getHosts()
  *
  * @return void
  */
 public function testGetHosts()
 {
     $MediaEmbed = new MediaEmbed();
     $hosts = $MediaEmbed->getHosts();
     $this->assertTrue(count($hosts) > 50);
     $hosts = $MediaEmbed->getHosts(['myvideo', 'youtube']);
     $this->assertTrue(count($hosts) === 2);
 }