GithubHelp home page GithubHelp logo

opengraph's Introduction

Open Graph Protocol helper for PHP

A small library for making accessing of Open Graph Protocol data easier

Note

Keys with a dash (-) in the name are converted to _ for easy access as a property in PHP

Required Extensions

  • DOM for parsing

Usage

require_once('OpenGraph.php');

$graph = OpenGraph::fetch('http://www.rottentomatoes.com/m/10011268-oceans/');
var_dump($graph->keys());
var_dump($graph->schema);

foreach ($graph as $key => $value) {
	echo "$key => $value";
}

opengraph's People

Contributors

aramzs avatar mitchellmckenna avatar scottmac avatar sebastianbergmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opengraph's Issues

Laravel Package

Would you mind if I distributed this package as a Laravel4 package via Composer? I'd give you due credit, I just think it'd be easier to install this via composer.

☠️ Alternatives for this dead project ☠️

This project is very obviously not being maintained yet there are 146 forks at this time. Does anybody know if there is somebody who is maintaining this in their fork or if there is a similar project available?

accessing the description and images for google.com and ebay

in testing my code here i have found that the opengraph code is not able to retrieve a description or image for google.com or ebay (and others).
i am unclear on what is the appropriate way to do this and searching in google's search engine for answers is not achieving much due to so many pages existing on the topic of how to get google to index our own pages.
anyone?

It was working, but not I get an "Uncaught Error: Call to a member function keys() on boolean in"

Hello Everyone,

It works on 99.9% of sites and it was working on PlayBuzz.com earlier today, but it stopped working a few hours ago. The video below shows the results (working) from a few sites and then it shows the error I get when I update the URL to a Playbuzz page.

I'm trying to figure out what is wrong so I can make the necessary adjustments and do so again in the future if the target site changes their code again.

Thanks for your help!
Todd

FYI - I'm not a developer, I'm I high school web design teacher. More of a "weekend warrior" that learns everything from GitHub and Stackoverflow.

1 Minute Video Demonstrating The Error
https://youtu.be/5K1WTEwPCzk

ERROR
Fatal error: Uncaught Error: Call to a member function keys() on boolean in /homepages/20/d627076981/htdocs/testYourKnowledgeNet/test/index.php:24 Stack trace: #0 {main} thrown in /homepages/20/d627076981/htdocs/testYourKnowledgeNet/test/index.php on line 24

Preloaded Images

Doesn't seem to get anything if images are preloaded. Not sure why??

Edit: may not be preloads, but may be a problem with gifs. Still thinking...

Why not use curl instead ?

Simple change... A lot more compatible, and far less compatibility issues than file_get_contents (allow_url_fopen must be on, other issues I couldn't even sort out on fetching remote content via file_get_contents() on my production server)...

Just a suggestion, as I made the change on my own and things work fine for me now.

/**
* Fetches a URI and parses it for Open Graph data, returns
* false on error.
*
* @param $URI URI to page to parse for Open Graph data
* @return OpenGraph
*/
static public function fetch($URI) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
curl_close($ch);
return self::_parse($contents);
}

Changing _parse() to public

file_get_contents() is disabled by some hosting-services... if parse() would be public, anyone could choose how to "fetch" (file_get_content, CURL, ...) the html-code himself.

Add fallback to images in the body if OGP tags are missing

I wanted to get others thoughts on adding a feature where if no OGP tags are present, the library could fallback to images in the HTML.

This should be fairly easily done by selecting all img tags in the body using DOMDocument. An example of using DOMDocument like this is where I do fall back for image_src if an OGP image tag is not provided: 155c2f4

So that tiny images aren't included (such as images unrelated to the specific page, eg. user profile pictures), I suggest the library not return images smaller than say 150x150 px.

Some people may not want this feature to occur however, since unrelated images may be found in the body, so there should be a optional parameter you can pass to fetch() to disable it.

This is also related to issue #7 to update the library to return an array for OGP tag types which allow multiple instances of the tag, such as images/video.

This could likely be done for other tags like video as well.

laravel 5 package

Can I create a laravel 5 package and distributed it as laravel package..it would be good and easy to use using composer.

This PHP library dont work if called from Android

Hi,

Im calling this code from Android app, passing the URL to the php script, it always fails by not fetching the HTML from the given URL. But when i call it from PC app, it works.

Any idea what would cause that?

Repeated tags

Youtube and Vimeo, both sites are duplicating 'go:video:url' tags, the first one works well, but the another one downloads a file, so is not working for embedding purposes.

How can i get the first one instead the last one?

This is the YouTube example:

<meta property="og:video:url" content="https://www.youtube.com/embed/C28onNQMXNU">
<meta property="og:video:secure_url" content="https://www.youtube.com/embed/C28onNQMXNU">
<meta property="og:video:type" content="text/html">
<meta property="og:video:width" content="480">
<meta property="og:video:height" content="360">
<meta property="og:video:url" content="http://www.youtube.com/v/C28onNQMXNU?version=3&amp;autohide=1">
<meta property="og:video:secure_url" content="https://www.youtube.com/v/C28onNQMXNU?version=3&amp;autohide=1">
<meta property="og:video:type" content="application/x-shockwave-flash">

Handling Multiple OGP Image tags

The OGP Arrays section says websites can have multiple images associated with a page and that the "first tag (from top to bottom) is given preference during conflicts".

Currently in the library it looks like if the page has multiple og:image tags, each one will overwrite the previous, so when you call $graph->image you will only get the last og:image value back. Images and any other tags that can have multiple values (eg. videos and audio) should probably return an array.

cURL followlocation issue

Hi,

I've just moved to a cloud hosting environment, and at this time don't believe I have direct access to my php.ini for modifications.

Since moving; I am getting the following PHP warning:
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set in /var/../OpenGraph.php on line 56

Has anyone else encountered this, and or have a work around or solution?
UPDATE: This issue is primary due to settings on the server-side that I still can't resolve, and will attempt to use the workaround of just commenting out:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

Thanks in advance for any assistance, and to all those that contribute to this great open graph script.

javascript support

Hello,
Do you plan supporting javascript based sites ? I'm talking about single page applications.
Thank you,
Vincent

Add fallback for compressed html?

I tried using OpeGraph but it kept on failing.
Eventually I found out that the HTML that was fetched by the inserted url was actually compressed in gzip.

I fixed it temporarily and dirty by checking if the string "gzip" was present in the html and then run gzdecode() on the html..

Google+ image

Google+ uses the to get a thumbnail from sites and facebook also added to their code so it would be great to use this meta tag also. And most of the site stopped using the link rel='image_src' (The New York Times too) so now I think it's unnecessary.

I changed that part to this:

    if (!isset($page->values['image'])) {
        $domxpath = new DOMXPath($doc);
        $elements = $domxpath->query("//meta[@itemprop='image']");

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

I also tried to creat an automatic image finder but I got in truble with that.

Missing Underscore

Found a missing underscore right before values['image']:

    //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']");

This means the fallback isn't being used. Possibly better that way, anyway.

Edit: Not sure what the problem really is here.... I seem to have this code that works fine from a desktop but never works from my phone. Have cleared buffers, etc. Weird.

Fix for pages with multiple copies of tags

A web page may have multiple og:image tags, for example, so I made each _value[$key] into an array, and then values are pushed onto the array, like so:

            if (!is_array($page->_values[$key]))
            {
                $page->_values[$key] = array();
            }
            $page->_values[$key][] = $tag->getAttribute('value');

Of course there are a few keys that need to be explicitly handled, like this:

            if (!is_array($page->_values['title']))
            {
                $page->_values['title'] = array();
            }
            $page->_values['title'][] = $titles->item(0)->textContent;

Fails for https://itunes.apple.com/app/id560137110

<meta content="Borderlands 2 Official Map App" property="og:title" /><meta content="Get Borderlands 2 Official Map App on the App Store. See screenshots and ratings, and read customer reviews." property="og:description" /><meta content="App Store" property="og:site_name" /><meta content="http://a1.mzstatic.com/us/r1000/103/Purple/v4/35/af/7c/35af7c66-92c6-4c22-96bd-b9f42baf3b71/mzm.ehmnocog.png" property="og:image" /><meta content="image/png" property="og:image:type" />

Encoding Issues

I thought it will handle encoding of Metadata and output it in UTF-8 but sadly it does not.
For an example below is my code that does not produce expected response:

<?php
header('Content-Type: text/html; charset=utf-8');
?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
require('OpenGraph.php');

// Test Domains: tudou.com, baidu.com, liveinternet.ru, cnzz.com, allegro.pl
$graph = OpenGraph::fetch('http://' . $_GET['domain']);
var_dump($graph->keys());
var_dump($graph->schema);

foreach ($graph as $key => $value) {
    echo "$key => $value";
}
?>

multiple properties

if property has more than one attribute:

<meta property="og:title dc:title" content="The Rock" />

the parser returns:

array('title dc:title' => 'The Rock')

and that causes problems with $og->title

Composer Support

Hey, are you thinking of writing in composer support? If not do you mind if I fork the project and do so myself?

Please check if cURL/SSL is installed and activated

/**
     * Fetches a URI and parses it for Open Graph data, returns
     * false on error.
     *
     * @param $URI    URI to page to parse for Open Graph data
     * @return OpenGraph
     */
    static public function fetch($URI) {


        try {
            $curl = curl_init($URI);
            if ($curl === false) {
                throw new Exception('failed to initialize');
            }


            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, true);
            curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

            $response = curl_exec($curl);
            if ($response === false) {
                throw new Exception(curl_error($curl), curl_errno($curl));
            }

            curl_close($curl);

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

            trigger_error(sprintf(
                'Curl failed with error #%d: %s',
                $e->getCode(), $e->getMessage()),
                E_USER_ERROR);
        }
    }

This got it working for me after checking cURL manually and then with problems with the SSL.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.