Skip to content
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

Fix UTF-8 encoding response #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 11 additions & 11 deletions OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Original can be found at https://github.com/scottmac/opengraph/blob/master/OpenGraph.php

*/

class OpenGraph implements Iterator
Expand Down Expand Up @@ -82,8 +82,8 @@ static private function _parse($HTML) {
$old_libxml_error = libxml_use_internal_errors(true);

$doc = new DOMDocument();
$doc->loadHTML($HTML);
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $HTML);

libxml_use_internal_errors($old_libxml_error);

$tags = $doc->getElementsByTagName('meta');
Expand All @@ -94,15 +94,15 @@ static private function _parse($HTML) {
$page = new self();

$nonOgDescription = null;

foreach ($tags AS $tag) {
if ($tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
$page->_values[$key] = $tag->getAttribute('content');
}
//Added this if loop to retrieve description values from sites like the New York Times who have malformed it.

//Added this if loop to retrieve description values from sites like the New York Times who have malformed it.
if ($tag ->hasAttribute('value') && $tag->hasAttribute('property') &&
strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
Expand All @@ -112,7 +112,7 @@ static private function _parse($HTML) {
if ($tag->hasAttribute('name') && $tag->getAttribute('name') === 'description') {
$nonOgDescription = $tag->getAttribute('content');
}

}
//Based on modifications at https://github.com/bashofmann/opengraph/blob/master/src/OpenGraph/OpenGraph.php
if (!isset($page->_values['title'])) {
Expand Down Expand Up @@ -140,7 +140,7 @@ static private function _parse($HTML) {
}

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

return $page;
}

Expand All @@ -155,7 +155,7 @@ public function __get($key) {
if (array_key_exists($key, $this->_values)) {
return $this->_values[$key];
}

if ($key === 'schema') {
foreach (self::$TYPES AS $schema => $types) {
if (array_search($this->_values['type'], $types)) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public function hasLocation() {
if (array_key_exists('latitude', $this->_values) && array_key_exists('longitude', $this->_values)) {
return true;
}

$address_keys = array('street_address', 'locality', 'region', 'postal_code', 'country_name');
$valid_address = true;
foreach ($address_keys AS $key) {
Expand Down