/** * Convert a single URL, assumes $url has been verified to be * a real URL * * @param string $url */ public function convertUrl($url) { $oembed = Oembed::get_oembed_from_url($url, false, $this->oembedOptions); if ($oembed) { return array('Title' => '', 'Content' => $oembed->forTemplate()); } $graph = OpenGraph::fetch($url); if ($graph) { foreach ($graph as $key => $value) { $data[$key] = Varchar::create_field('Varchar', $value); } if (isset($data['url'])) { return array('Title' => $graph->Title, 'Content' => MicroPost::create()->customise($data)->renderWith('OpenGraphPost')); } } // get the post and take its <title> tag at the very least $service = new RestfulService($url); $response = $service->request(); if ($response && $response->getStatusCode() == 200) { if (preg_match('/<title>(.*?)<\\/title>/is', $response->getBody(), $matches)) { $title = Convert::raw2xml(trim($matches[1])); return array('Title' => $title, 'Content' => "<a href='{$url}'>{$title}</a>"); } } }
public static function getOpenGraphData($url) { $returnValues = array(); //obtengo los datos del enlace con su imagen titulo y descripcion haaa y la url $graph = OpenGraph::fetch($url); foreach ($graph as $key => $value) { switch ($key) { case 'title': $returnValues['title'] = $value; break; case 'description': $returnValues['description'] = $value; break; case 'site_name': $returnValues['siteName'] = $value; break; case 'image': $returnValues['image'] = $value; break; } } //Checking video link if youtube or vimeo $host = parse_url($url, PHP_URL_HOST); if ($host == 'vimeo.com') { $path = parse_url($url, PHP_URL_PATH); $clip_id = substr($path, 1); $returnValues['videoUrl'] = 'http://vimeo.com/moogaloop.swf?clip_id=' . $clip_id; } if ($host == 'www.youtube.com') { $query = parse_url($url, PHP_URL_QUERY); $v = explode("=", $query); $returnValues['videoUrl'] = 'http://www.youtube.com/v/' . $v[1] . '?version=3&autohide=1'; } if ($host == 'youtu.be') { $path = parse_url($url, PHP_URL_PATH); $returnValues['videoUrl'] = 'http://www.youtube.com/v' . $path . '?version=3&autohide=1'; } //veo si alguno de los campos esta vacio y mando la funcion con curl if (isset($returnValues['title']) && isset($returnValues['description']) && isset($returnValues['image']) && isset($returnValues['siteName'])) { return $returnValues; } $arr = OpenGraph::wget($url); if (!isset($returnValues['title'])) { $returnValues['title'] = $arr['title']; } if (!isset($returnValues['description'])) { $returnValues['description'] = $arr['description']; } if (!isset($returnValues['image'])) { $returnValues['image'] = $arr['og:image']; } if (!isset($returnValues['images'])) { $returnValues['images'] = $arr['images'][0]; } return $returnValues; }
/** * @param \Zend\Uri\Uri $uri */ public function parse(Uri $uri) { $this->url = $uri->toString(); $html = \OpenGraph::fetch($uri->toString()); // open graph $this->id = basename($uri->getPath()); $this->title = $html->title; $this->description = $html->description; $this->image = urlencode($html->image); }
/** * @param \Zend\Uri\Uri $uri */ public function parse(Uri $uri) { $this->url = $uri->toString(); $html = \OpenGraph::fetch($this->url); $query = $uri->getQueryAsArray(); // open graph $this->id = $query['v']; $this->title = $html->title; $this->description = $html->description; $this->image = urlencode($html->image); }
public static function analyze_url($url) { $graph = OpenGraph::fetch($url); $keys = array('title', 'type', 'image', 'url', 'site_name', 'description'); $returns = array(); foreach ($keys as $key) { if (!isset($graph->{$key})) { continue; } $returns[$key] = $graph->{$key}; } if ($returns && empty($returns['url'])) { $returns['url'] = $url; } return $returns; }
/** * Extract URL from a string and fetch open graph data * * @return string */ public function getUrl($string) { // Save data for AJAX response $response = []; // Extract URL from comment string preg_match(self::URL_REGEX, $string, $matches); if (!filter_var($matches[0], FILTER_VALIDATE_URL) === false) { // Fetch OpenGraph data $opengraph = OpenGraph::fetch($matches[0]); $keys = $opengraph->keys(); // Save OpenGraph data in $response if (count($keys) > 0) { foreach ($keys as $key) { $response[$key] = $opengraph->__get($key); } } } return $response; }
public static function fetch_image_post($post_id = '', $link = '', $comment_fbid = '') { if (!$post_id || !$link) { return; } $image = ''; if ($comment_fbid) { $app_id = FB_APP_ID; $app_secret = FB_SECRET; $url = "https://graph.facebook.com/{$comment_fbid}?access_token={$app_id}|{$app_secret}"; $obj = json_decode(file_get_contents_curl($url)); if (isset($obj->image) && $obj->image) { $image = $obj->image->url; } } var_dump($image); die; if (!empty($image)) { $graph = OpenGraph::fetch($link); var_dump($graph); die; } }
/* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('home'); }); Route::post('fetch', function () { require_once 'app/Classes/OpenGraph.php'; $graph = OpenGraph::fetch(Input::get('url')); foreach ($graph as $key => $value) { $output[$key] = $value; } return $output; }); Route::post('sendtweet', function () { $file = file_get_contents(Input::get('image')); $file = base64_encode($file); $uploaded_media = Twitter::uploadMedia(['media_data' => $file]); return Twitter::postTweet(['status' => Input::get('text'), 'media_ids' => $uploaded_media->media_id_string]); }); Route::get('twitter/login', ['as' => 'twitter.login', function () { // your SIGN IN WITH TWITTER button should point to this route $sign_in_twitter = true; $force_login = false;
/** * Validate changes to an article before committing changes * @since Version 3.4 * @return boolean */ public function validate() { if (empty($this->title)) { throw new Exception("Validation failed: title is empty"); return false; } if (empty($this->blurb)) { throw new Exception("Validation failed: blurb is empty"); return false; } if (empty($this->body)) { throw new Exception("Validation failed: body is empty"); return false; } if (!isset($this->Author) || !$this->Author instanceof User) { $this->Author = new User($this->user_id); } /** * Try to get the featured image from OpenGraph tags */ if ($this->source && !$this->featured_image) { require_once "vendor" . DS . "scottmac" . DS . "opengraph" . DS . "OpenGraph.php"; $graph = \OpenGraph::fetch($this->source); #printArray($graph->keys()); #printArray($graph->schema); foreach ($graph as $key => $value) { if ($key == "image" && strlen($value) > 0) { $this->featured_image = $value; } } } return true; }
/** * Get html data with OpenGrap passing the Url * @param $link url * @return string data html */ public static function readContentWithOpenGraph($link) { $graph = OpenGraph::fetch($link); if (!$graph) { return false; } $url = $graph->url; $image = $graph->image; $domain = empty($url) ? parse_url($link) : parse_url($url); $domain = $domain['scheme'] . '://' . $domain['host']; // Trick to verify if the Image Url Exist because of some bad metatag dev if (self::verifyUrl($image) == false) { if (!($image[0] == '/')) { $domain = $domain . '/'; } $image = $domain . $image; } $title = $graph->title; $html = '<div class="thumbnail">'; $html .= '<a target="_blank" href="' . $link . '"><h3>' . $title . '</h3>'; $html .= empty($image) ? '' : '<img alt="" src="' . $image . '" /></a>'; $html .= empty($graph->description) ? '' : '<p class="description">' . $graph->description . '</p>'; $html .= '<a href="' . $link . '">' . $link . '</a>'; $html .= '</div>'; return $html; }
/** * Validate changes to an article before committing changes * @since Version 3.4 * @return boolean * @throws \Exception if the article title is empty * @throws \Exception if the article blurb and lead are empty * @throws \Exception if the body and paragraphs AND source are empty * @throws \Exception if the article title is too long */ public function validate() { if (empty($this->title)) { throw new Exception("Validation failed: title is empty"); } if (empty($this->blurb) && empty($this->lead)) { throw new Exception("Validation failed: blurb is empty"); } if (empty($this->body) && empty($this->source) && empty($this->paragraphs)) { throw new Exception("Validation failed: body is empty"); } if (is_null($this->blurb) || !empty($this->lead)) { $this->blurb = ""; } if (is_null($this->body) || !empty($this->paragraphs)) { $this->body = ""; } if (is_null($this->paragraphs)) { $this->paragraphs = ""; } if (is_null($this->lead)) { $this->lead = ""; } if (!isset($this->Author) || !$this->Author instanceof User) { $this->Author = UserFactory::CreateUser($this->user_id); } if (!$this->date instanceof DateTime) { $this->date = new DateTime(); } if (!filter_var($this->approved)) { $this->approved = self::STATUS_UNAPPROVED; } if (is_null($this->source)) { $this->source = ""; } if (empty($this->unique_id)) { $this->unique_id = md5($this->title); } if (!is_bool($this->queued)) { $this->queued = false; } /** * Try to get the featured image from OpenGraph tags */ if ($this->source && !$this->featured_image) { require_once "vendor" . DS . "scottmac" . DS . "opengraph" . DS . "OpenGraph.php"; $graph = \OpenGraph::fetch($this->source); #printArray($graph->keys()); #printArray($graph->schema); foreach ($graph as $key => $value) { if ($key == "image" && strlen($value) > 0) { $this->featured_image = $value; } } } return true; }
/** * html with data OpenGrap * @param $link url * @return string data html */ public static function getHtmlByLink($link) { $graph = OpenGraph::fetch($link); $title = $graph->title; $html = '<div>'; $html .= '<a target="_blank" href="'.$link.'"><h3>'.$title.'</h3>'; $html .= empty($graph->image) ? '' : '<img alt="" src="'.$graph->image.'" height="160" ></a>'; $html .= empty($graph->description) ? '' : '<div>'.$graph->description.'</div>'; $html .= "</div>"; return $html; }
public function testFetchReturnsFalseForWebsiteWithNoOpenGraphMetadata() { $this->assertEquals(FALSE, OpenGraph::fetch('http://www.example.org/')); }
<?php $embedded = ''; $body = Idno\Core\site()->triggerEvent('url/expandintext', ['object' => $vars['object']], $vars['object']->body); if (preg_match_all('/\\s+(https?:\\/\\/[^\\s]+)/i', $body, $matches)) { foreach ($matches[1] as $m) { // Parse with opengraph $graph = OpenGraph::fetch($m); $embedded .= "<div class=\"OpenGraphContainer\">"; $embedded .= "<a href=\"{$m}\" target=\"_blank\"><div>"; if ($graph->image) { $embedded .= "<div class=\"OpenGraphImage\">" . "<img width=\"100px\"src=\"{$graph->image}\"/></div>"; } else { $linkimg = Idno\Core\site()->config()->getDisplayURL() . "IdnoPlugins/OpenGraphConsumer/images/hyperlink.png"; $embedded .= "<div class=\"OpenGraphImage\">" . "<img width=\"100px\" src=\"{$linkimg}\"/></div>"; } $embedded .= "<div class=\"OpenGraphContentOuter\">"; $embedded .= "<div class=\"OpenGraphContent\">"; if ($graph->title) { $embedded .= "<strong>{$graph->title}</strong>"; } if ($graph->description) { // Trim it down if (strlen($graph->description) > 300) { $desc = substr($graph->description, 0, 300) . "..."; } else { $desc = $graph->description; } $embedded .= "<p style=\"font-size:0.7em;\">{$desc}</p>"; } $embedded .= "</div>";
/** * Displays the page for generating HTML code for pocket entry */ public function code_generation_page() { ?> <div class="wrap"> <h2 class="pkt-nws-gnrtr"><?php _e('Retrieve Items in Pocket and Generate HTML Code', self::DOMAIN); ?> </h2> <p><?php _e('Specify search condition for Pocket data retrieval and push the button below.', self::DOMAIN); ?> </p> <div class="pkt-nws-gnrtr"> <form action="" method="post"> <div> <label><?php _e('State', self::DOMAIN); ?> </label><br /> <select name="<?php echo PocketUtil::OPT_STATE; ?> " class="dropdown"> <option value="all" selected><?php _e('all (both unread and archived items)', self::DOMAIN); ?> </option> <option value="unread"><?php _e('only unread items', self::DOMAIN); ?> </option> <option value="archive"><?php _e('only archived items', self::DOMAIN); ?> </option> </select> </div> <div> <label><?php _e('Favorite', self::DOMAIN); ?> </label><br /> <select name="<?php echo PocketUtil::OPT_FAVORITE; ?> " class="dropdown"> <option value ="" selected><?php _e('all (both un-favorited and favorited items)', self::DOMAIN); ?> </option> <option value="0"><?php _e('only un-favorited items', self::DOMAIN); ?> </option> <option value="1"><?php _e('only favorited items', self::DOMAIN); ?> </option> </select> </div> <div> <label><?php _e('Tag', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_TAG; ?> " size="60" value="" /> </div> <div> <label><?php _e('Content Type', self::DOMAIN); ?> </label><br /> <select name="<?php echo PocketUtil::OPT_CONTENT_TYPE; ?> " class="dropdown"> <option value ="" selected><?php _e('all', self::DOMAIN); ?> </option> <option value="article"><?php _e('only articles', self::DOMAIN); ?> </option> <option value="video"><?php _e('only videos or articles with embedded videos', self::DOMAIN); ?> </option> <option value="image"><?php _e('only images', self::DOMAIN); ?> </option> </select> </div> <div> <label><?php _e('Sort', self::DOMAIN); ?> </label><br /> <select name="<?php echo PocketUtil::OPT_SORT; ?> " class="dropdown"> <option value ="newest" selected><?php _e('items in order of newest to oldest', self::DOMAIN); ?> </option> <option value="oldest"><?php _e('items in order of oldest to newest', self::DOMAIN); ?> </option> <option value="title"><?php _e('items in order of title alphabetically', self::DOMAIN); ?> </option> <option value="site"><?php _e('items in order of URL alphabetically', self::DOMAIN); ?> </option> </select> </div> <div> <label><?php _e('Search', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_SEARCH; ?> " size="60" value="" /> </div> <div> <label><?php _e('Domain', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_DOMAIN; ?> " size="60" value="" /> </div> <div> <label><?php _e('Since (YYYY-MM-DD HH24:MM)', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_SINCE; ?> " size="60" value="" /> </div> <div> <label><?php _e('Count', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_COUNT; ?> " size="60" value="" /> </div> <div> <label><?php _e('Offset', self::DOMAIN); ?> </label><br /> <input type="text" class="text" name="<?php echo PocketUtil::OPT_OFFSET; ?> " size="60" value="" /> </div> <input type="hidden" name="action" value="generate" /> <div> <input type="submit" class="button button-primary" value="<?php _e('Generate', self::DOMAIN); ?> " /> </div> </form> </div> </div> <br /> <?php if ($_POST["action"] === 'generate') { $consumer_key = get_option(self::DB_CONSUMER_KEY); $access_token = get_option(self::DB_ACCESS_TOKEN); $format = get_option(self::DB_FORMAT); $state = $_POST[PocketUtil::OPT_STATE]; $favorite = $_POST[PocketUtil::OPT_FAVORITE]; $tag = $_POST[PocketUtil::OPT_TAG]; $contentType = $_POST[PocketUtil::OPT_CONTENT_TYPE]; $sort = $_POST[PocketUtil::OPT_SORT]; $search = $_POST[PocketUtil::OPT_SEARCH]; $domain = $_POST[PocketUtil::OPT_DOMAIN]; $since = $_POST[PocketUtil::OPT_SINCE]; $count = $_POST[PocketUtil::OPT_COUNT]; $offset = $_POST[PocketUtil::OPT_OFFSET]; if (isset($state) && $state) { $option_params[PocketUtil::OPT_STATE] = $state; } if ($favorite == 0 || $favorite == 1) { $option_params[PocketUtil::OPT_FAVORITE] = $favorite; } if (isset($tag) && $tag) { $option_params[PocketUtil::OPT_TAG] = $tag; } if (isset($contentType) && $contentType) { $option_params[PocketUtil::OPT_CONTENT_TYPE] = $contentType; } if (isset($sort) && $sort) { $option_params[PocketUtil::OPT_SORT] = $sort; } if (isset($search) && $search) { $option_params[PocketUtil::OPT_SEARCH] = $search; } if (isset($domain) && $domain) { $option_params[PocketUtil::OPT_DOMAIN] = $domain; } if (isset($since) && $since) { $gmt_time = get_gmt_from_date($since); $option_params[PocketUtil::OPT_SINCE] = strtotime($gmt_time); } if (isset($count) && $count) { $option_params[PocketUtil::OPT_COUNT] = $count; } if (isset($offset) && $offset) { $option_params[PocketUtil::OPT_OFFSET] = $offset; } $option_params[PocketUtil::OPT_DETAIL_TYPE] = 'complete'; //$debug_mode = true; if (isset($debug_mode) && $debug_mode) { echo 'consumer_key: ' . $consumer_key . '<br />'; echo 'access_token: ' . $access_token . '<br />'; echo 'state: ' . $state . '<br />'; echo 'favorite: ' . $favorite . '<br />'; echo 'tag: ' . $tag . '<br />'; echo 'contentType: ' . $contentType . '<br />'; echo 'sort: ' . $sort . '<br />'; echo 'search: ' . $search . '<br />'; echo 'domain: ' . $domain . '<br />'; echo 'since: JST-> ' . $since . ' GMT-> ' . $gmt_time . '<br />'; echo 'count: ' . $count . '<br />'; echo 'offset: ' . $offset . '<br />'; echo 'Default timezone: ' . date_default_timezone_get() . '<br />'; echo 'Local timezone: ' . get_option('timezone_string') . '<br />'; echo 'Difference in time from default timezone: ' . get_option('gmt_offset') . '<br />'; echo 'Array of option parameters:<br />'; print_r($option_params); } $pocket_util = new PocketUtil(); $pocket_body = $pocket_util->retrieveItem($consumer_key, $access_token, $option_params); $feedly_util = new FeedlyUtil(); if (!empty($pocket_body)) { //Check if there is mached data. if (!empty($pocket_body->list)) { echo '<div class="wrap">'; echo '<h2 class="pkt-nws-gnrtr">' . __('Generated HTML Code', self::DOMAIN) . '</h2>'; echo '<p>' . __('Generated HTML code is as follows. Copy and paste it into your post.', self::DOMAIN) . '</p>'; echo '<div class="pkt-nws-gnrtr">'; echo '<label>' . __('HTML Code', self::DOMAIN) . '</label><br />'; echo '<textarea class="text" style="width:500px;height:300px;">'; foreach ($pocket_body->list as $pocket_item) { //Post URL $html_code = str_replace(self::FRMT_POST_URL, $pocket_item->{PocketUtil::REF_RESOLVED_URL}, stripslashes($format)); //Post title $html_code = str_replace(self::FRMT_POST_TITLE, $pocket_item->{PocketUtil::REF_RESOLVED_TITLE}, $html_code); //Site name and site URL if (strpos($format, self::FRMT_SITE_NAME) !== false || strpos($format, self::FRMT_SITE_URL) !== false) { $url_util = new URLUtil($pocket_item->{PocketUtil::REF_RESOLVED_URL}); $feedly_info_flag = false; do { $feedly_body = $feedly_util->getSiteInfo($url_util->getURL()); $site_name = $feedly_body->results[0]->{FeedlyUtil::REF_SITE_NAME}; $site_url = $feedly_body->results[0]->{FeedlyUtil::REF_SITE_URL}; if (isset($site_name) && isset($site_url)) { $html_code = str_replace(self::FRMT_SITE_NAME, $site_name, $html_code); $html_code = str_replace(self::FRMT_SITE_URL, $site_url, $html_code); $feedly_info_flag = true; break; } //Check next path existence } while ($url_util->next()); if (!$feedly_info_flag) { //Site URL (URL of top page) retrieval $site_url = $pocket_util->getBaseUrl($pocket_item->{PocketUtil::REF_RESOLVED_URL}); $html_code = str_replace(self::FRMT_SITE_URL, $site_url, $html_code); //Information retrieval of OGP $graph = OpenGraph::fetch($pocket_item->{PocketUtil::REF_RESOLVED_URL}); if (isset($graph->site_name)) { // Content of og:site_name $html_code = str_replace(self::FRMT_SITE_NAME, $graph->site_name, $html_code); } else { // Content of title in top page $site_name = $pocket_util->getSiteName($pocket_item->{PocketUtil::REF_RESOLVED_URL}); $html_code = str_replace(self::FRMT_SITE_NAME, $site_name, $html_code); } } } //Post excerpt if (strpos($format, self::FRMT_POST_EXCERPT) !== false) { if (isset($pocket_item->{PocketUtil::REF_EXCERPT})) { // Content of excerpt in Pocket data $html_code = str_replace(self::FRMT_POST_EXCERPT, $pocket_item->{PocketUtil::REF_EXCERPT}, $html_code); } else { //Information retrieval of OGP $graph = OpenGraph::fetch($pocket_item->{PocketUtil::REF_RESOLVED_URL}); // Content of og:description $html_code = str_replace(self::FRMT_POST_EXCERPT, $graph->description, $html_code); } } //Post image if (strpos($format, self::FRMT_POST_IMAGE) !== false) { if (isset($graph->image)) { //Information retrieval of OGP $graph = OpenGraph::fetch($pocket_item->{PocketUtil::REF_RESOLVED_URL}); $html_code = str_replace(self::FRMT_POST_IMAGE, $graph->image, $html_code); } } echo htmlspecialchars($html_code); } echo '</textarea>'; echo '</div>'; echo '</div>'; } else { echo '<div class="pkt-nws-gnrtr">'; echo '<span class="red">' . __('INFO: Pocket data matching your specified condition was not found.', self::DOMAIN) . '</span>'; echo '</div>'; } } else { echo '<div class="pkt-nws-gnrtr">'; echo '<span class="red">' . __('ERROR: Pocket data retrieval failed.', self::DOMAIN) . '</span>'; echo '</div>'; } } }
<?php /** * Link Post Format template */ require_once get_template_directory() . '/inc/OpenGraph.php'; $graph = OpenGraph::fetch(get_field('external_url')); if (!empty($graph)) { $og_title = $graph->__isset('title') ? $graph->__get('title') : ''; $og_desc = $graph->__isset('description') ? $graph->__get('description') : ''; $og_image = $graph->__isset('image') ? $graph->__get('image') : ''; } $page_id = get_option('page_for_posts'); ?> <h1 class="no-pad"><?php the_title(); ?> </h1> <hr style="border-color: <?php __the_field('color_theme', 'esc_attr', $page_id); ?> ;" /> <?php the_content(); ?> <hr style="border-color: <?php __the_field('color_theme', 'esc_attr', $page_id); ?> ;" /> <div class="row open-graph-data">
/** * Get OpenGraph tags from a specified URL * Really stupid and elaborate Memcached expiry handling is due to a bug in Debian's PHP5-Memcached package * * @since Version 3.10.0 * @param string $url * @return array */ public static function GetOpenGraphTags($url) { $Memcached = AppCore::GetMemcached(); $mckey = md5($url); if ($result = $Memcached->fetch($mckey)) { $exp = $Memcached->fetch(sprintf("%s-exp", $mckey)); if ($exp < time()) { $Memcached->delete($mckey); $Memcached->delete(sprintf("%s-exp", $mckey)); $result = false; } } if (!$result) { /** * Ensure our OG handler is loaded */ require_once "vendor" . DS . "scottmac" . DS . "opengraph" . DS . "OpenGraph.php"; $graph = \OpenGraph::fetch($url); $result = array(); foreach ($graph as $key => $value) { $result[$key] = $value; } $Memcached->save($mckey, $result, 0); // 0 or will not cache $Memcached->save(sprintf("%s-exp", $mckey), strtotime("+1 day"), 0); // alternate method of specifying expiry } return $result; }