Skip to content

Bringing my base up to date #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 8, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,25 @@ class OpenGraph implements Iterator
* @return OpenGraph
*/
static public function fetch($URI) {
return self::_parse(file_get_contents($URI));
$curl = curl_init($URI);

curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

$response = curl_exec($curl);

curl_close($curl);

if (!empty($response)) {
return self::_parse($response);
} else {
return false;
}
}

/**
Expand Down Expand Up @@ -107,6 +125,20 @@ static private function _parse($HTML) {
$page->_values['description'] = $nonOgDescription;
}

//Fallback to use image_src if ogp::image isn't set.
if (!isset($page->values['image'])) {
$domxpath = new DOMXPath($doc);
$elements = $domxpath->query("//link[@rel='image_src']");

if ($elements->length > 0) {
$domattr = $elements->item(0)->attributes->getNamedItem('href');
if ($domattr) {
$page->_values['image'] = $domattr->value;
$page->_values['image_src'] = $domattr->value;
}
}
}

if (empty($page->_values)) { return false; }

return $page;
Expand Down Expand Up @@ -178,4 +210,4 @@ public function current() { return current($this->_values); }
public function key() { return key($this->_values); }
public function next() { next($this->_values); ++$this->_position; }
public function valid() { return $this->_position < sizeof($this->_values); }
}
}