Created
June 4, 2015 19:31
-
-
Save thepsion5/673fd3828f8205fd47d5 to your computer and use it in GitHub Desktop.
The code from http://w3lessons.info/2015/06/01/facebook-url-expander-with-jquery-ajax-and-php/ but less terrible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FacebookUrlHelper | |
{ | |
private $graphApiUrl; | |
private $fbHostname; | |
public function __construct($graphApiUrl = 'http://graph.facebook.com', $fbHostname = 'facebook.com') | |
{ | |
$this->graphApiUrl = $graphApiUrl; | |
$this->fbHostname = $fbHostname; | |
} | |
public function getCoverImage($pageId) | |
{ | |
$pageData = $this->getDataFromGraphApi("$pageId?fields=cover"); | |
return $pageData->cover->source; | |
} | |
public function getProfilePicture($pageId) | |
{ | |
return $this->getDataFromGraphApi("$pageId/picture"); | |
} | |
public function isValidUrl($facebookUrl) | |
{ | |
if(!filter_var($facebookUrl, FILTER_VALIDATE_URL)) { | |
return false; | |
} | |
$hostname = str_replace('www.', '', parse_url($facebookUrl, PHP_URL_HOST) ); | |
return ($hostname === $this->fbHostname); | |
} | |
public function getDataFromUrl($facebookUrl, $contentTypeKey = '') | |
{ | |
$path = $this->getApiPathFromUrl($facebookUrl); | |
$data = $this->getDataFromGraphApi($path); | |
if($contentTypeKey) { | |
$data[$contentTypeKey] = $this->getTypeFromData($data); | |
} | |
return $data; | |
} | |
public function getTypeFromData($apiData) | |
{ | |
$hasLikes = isset($apiData->likes); | |
$hasId = isset($apiData->id); | |
if($hasLikes && $hasId) { | |
return 'page'; | |
} elseif($hasId) { | |
return 'profile'; | |
} else { | |
return 'other'; | |
} | |
} | |
private function getApiPathFromUrl($facebookUrl) | |
{ | |
if(!$this->isValidUrl($facebookUrl)) { | |
throw new \InvalidArgumentException("The URL [$facebookUrl] is not a valid Facebook URL."); | |
} | |
$urlParts = parse_url($facebookUrl); | |
$path = !empty($urlParts['path']) ? $urlParts['path'] : ''; | |
$path .= !empty($urlParts['query']) ? '?' . $urlParts['query'] : ''; | |
return $path; | |
} | |
private function getDataFromGraphApi($path) | |
{ | |
$url = $this->graphApiUrl . '/' . ltrim($path, '/'); | |
$response = file_get_contents($url); | |
if(!$response) { | |
throw new InvalidArgumentException("Unable to load data from the Graph API URL [$url]."); | |
} | |
return json_decode($response); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$facebook = new FacebookUrlHelper(); | |
if($facebook->isValidUrl($url)) { | |
$data = $facebook->getDataFromUrl($url, '_type'); | |
if($type == 'profile') { | |
echo get_profile_box($path, $data['name']); | |
} elseif($type == 'page') { | |
echo sprintf('<div class="fb-page" data-href="%s" data-width="500"></div>', $url); | |
} else { | |
echo sprintf('<div class="fb-post" data-href="%s"></div>', $url); | |
} | |
} | |
echo '<img src="' . $facebook->getCoverImage('itzurkarthi') . '">'; | |
echo '<img src="' . $facebook->getProfilePicture('itzurkarthi') . '">'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment