Skip to content

Commit 3d2180e

Browse files
committed
Merge pull request #2 from scottmac/master
Bringing my base up to date
2 parents 8137872 + faba415 commit 3d2180e

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

OpenGraph.php

+34-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,25 @@ class OpenGraph implements Iterator
5050
* @return OpenGraph
5151
*/
5252
static public function fetch($URI) {
53-
return self::_parse(file_get_contents($URI));
53+
$curl = curl_init($URI);
54+
55+
curl_setopt($curl, CURLOPT_FAILONERROR, true);
56+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
57+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58+
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
59+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
60+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
61+
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
62+
63+
$response = curl_exec($curl);
64+
65+
curl_close($curl);
66+
67+
if (!empty($response)) {
68+
return self::_parse($response);
69+
} else {
70+
return false;
71+
}
5472
}
5573

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

128+
//Fallback to use image_src if ogp::image isn't set.
129+
if (!isset($page->values['image'])) {
130+
$domxpath = new DOMXPath($doc);
131+
$elements = $domxpath->query("//link[@rel='image_src']");
132+
133+
if ($elements->length > 0) {
134+
$domattr = $elements->item(0)->attributes->getNamedItem('href');
135+
if ($domattr) {
136+
$page->_values['image'] = $domattr->value;
137+
$page->_values['image_src'] = $domattr->value;
138+
}
139+
}
140+
}
141+
110142
if (empty($page->_values)) { return false; }
111143

112144
return $page;
@@ -178,4 +210,4 @@ public function current() { return current($this->_values); }
178210
public function key() { return key($this->_values); }
179211
public function next() { next($this->_values); ++$this->_position; }
180212
public function valid() { return $this->_position < sizeof($this->_values); }
181-
}
213+
}

0 commit comments

Comments
 (0)