Giter VIP home page Giter VIP logo

opengraph's Introduction

fusonic/opengraph

Latest Stable Version Total Downloads Build Status License

A simple library to read Open Graph data from the web and generate HTML code to publish your own Open Graph objects. A fallback mode enables you to read data from websites that do not implement the Open Graph protocol.

Using this library you can easily retrieve stuff like meta data, video information from YouTube or Vimeo or image information from Flickr without using site-specific APIs since they all implement the Open Graph protocol.

See ogp.me for information on the Open Graph protocol.

Requirements

Installation

The most flexible installation method is using Composer:

composer require fusonic/opengraph

Install composer and run install command:

curl -s http://getcomposer.org/installer | php
php composer.phar install

Once installed, include vendor/autoload.php in your script.

require "vendor/autoload.php";

Usage

Retrieve Open Graph data from a URL

use Fusonic\OpenGraph\Consumer;

$consumer = new Consumer($httpClient, $httpRequestFactory);
$object = $consumer->loadUrl("http://www.youtube.com/watch?v=P422jZg50X4");

// Basic information of the object
echo "Title: " . $object->title;                // Getting started with Facebook Open Graph
echo "Site name: " . $object->siteName;         // YouTube
echo "Description: " . $object->description;    // Originally recorded at the Facebook World ...
echo "Canonical URL: " . $object->url;          // http://www.youtube.com/watch?v=P422jZg50X4

// Images
$image = $object->images[0];
echo "Image[0] URL: " . $image->url             // https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg
echo "Image[0] height: " . $image->height       // null (May return height in pixels on other pages)
echo "Image[0] width: " . $image->width         // null (May return width in pixels on other pages)

// Videos
$video = $object->videos[0];
echo "Video URL: " . $video->url                // http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1
echo "Video height: " . $video->height          // 1080
echo "Video width: " . $video->width            // 1920
echo "Video type: " . $video->type              // application/x-shockwave-flash

There are some more properties but these are the basic and most commonly used ones.

Publish own Open Graph data

use Fusonic\OpenGraph\Elements\Image;
use Fusonic\OpenGraph\Elements\Video;
use Fusonic\OpenGraph\Publisher;
use Fusonic\OpenGraph\Objects\Website;

$publisher = new Publisher();
$object = new Website();

// Basic information of the object
$object->title = "Getting started with Facebook Open Graph";
$object->siteName = "YouTube";
$object->description = "Originally recorded at the Facebook World ..."
$object->url = "http://www.youtube.com/watch?v=P422jZg50X4";

// Images
$image = new Image("https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg");
$object->images[] = $image;

// Videos
$video = new Video("http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1");
$video->height = 1080;
$video->width = 1920;
$video->type = "application/x-shockwave-flash";
$object->videos[] = $video;

// Generate HTML code
echo $publisher->generateHtml($object);
// <meta property="og:description"
//       content="Originally recorded at the Facebook World ...">
// <meta property="og:image:url"
//       content="https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg">
// <meta property="og:site_name"
//       content="YouTube">
// <meta property="og:type"
//       content="website">
// <meta property="og:url"
//       content="http://www.youtube.com/watch?v=P422jZg50X4">
// <meta property="og:video:url"
//       content="http://www.youtube.com/v/P422jZg50X4?version=3&amp;autohide=1">
// <meta property="og:video:height"
//       content="1080">
// <meta property="og:video:type"
//       content="application/x-shockwave-flash">
// <meta property="og:video:width"
//       content="1920">

HTML code is formatted just for displaying purposes. You may choose between HTML5/XHTML output using the $publisher->doctype property.

Running tests

You can run the test suite by running phpunit from the command line.

FAQ

I don't get any information from a webpage, but Facebook shows information for the same URL. What do I do wrong?

It seems that some pages (like Twitter) only publish OpenGraph information if Facebook's user agent string facebookexternalhit/1.1 is used (see #28). So you should configure your PSR-18 client to use this user agent string:

$client = new Psr18Client(new NativeHttpClient([ "headers" => [ "User-Agent" => "facebookexternalhit/1.1" ] ]));

License

This library is licensed under the MIT license.

opengraph's People

Contributors

mburtscher avatar adamwych avatar amrelnaggar avatar benallfree avatar dkarlovi avatar manavo avatar mrbase avatar

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.