2017 © Pedro Peláez
 

library opengraph

A Laravel package to fetch website Open Graph metadata.

image

shweshi/opengraph

A Laravel package to fetch website Open Graph metadata.

  • Tuesday, July 31, 2018
  • by imspg
  • Repository
  • 0 Watchers
  • 0 Stars
  • 39 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 105 % Grown

The README.md

Laravel OpenGraph , (*1)

OpenGraph is a laravel package to fetch Open Graph metadata of a website/link. , (*2)

Latest Version on Packagist Total Downloads Scrutinizer Code Quality StyleCI CodeFactor Build Status FOSSA Status , (*3)

Features

  • Easily fetch metadata of a URL. Laravel OpenGraph fetches all the metadata of a URL., (*4)

  • Supports language-specific metadata. Laravel OpenGraph can fetch metadata in a specific language if webpage supports., (*5)

  • Supports twitter metadata. Laravel OpenGraph supports twitter OG data too., (*6)

  • Verify image URL. Laravel OpenGraph verifies that the image URL in the image metadata is valid or not., (*7)

Demo

  curl https://laravelopengraph.herokuapp.com/api/fetch?url=ogp.me&allMeta=true&language=en_GB

How to use Laravel OpenGraph

An article can be found on the medium blog: https://hackernoon.com/how-to-fetch-open-graph-metadata-in-laravel-2d5d674904d7, (*8)

Documentation

https://opengraph.shashi.dev, (*9)

Installation

Perform the following operations in order to use this package, (*10)

  • Install via composer
composer require "shweshi/opengraph"

If you do not run Laravel 5.5 (or higher), then add the service provider in config/app.php:, (*11)

  • Add Service Provider Open config/app.php and add shweshi\OpenGraph\Providers\OpenGraphProvider::class, to the end of providers array:, (*12)

    'providers' => array(
      ....
      shweshi\OpenGraph\Providers\OpenGraphProvider::class,
    ),
    

    Next under the aliases array:, (*13)

    'aliases' => array(
      ....
      'OpenGraph' => shweshi\OpenGraph\Facades\OpenGraphFacade::class
    ),
    

If you do run the package on Laravel 5.5+, package auto-discovery takes care of the magic of adding the service provider., (*14)

Requirements

  • You need to install the DOM extension.

How to use

  • After following the above steps,, (*15)

    use OpenGraph;
    
    $data = OpenGraph::fetch("https://unsplash.com/");
    

    this will give you an array like this..., (*16)

    array (
      'title' => 'Beautiful Free Images & Pictures | Unsplash',
      'description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.',
      'type' => 'website',
      'url' => 'https://unsplash.com/',
      'image' => 'http://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0',
      'image:secure_url' => 'https://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0',
    )
    

    You can also pass an optional parameter either true or false along with URL. When set false it will only fetch basic metadata and in case of true it will fetch all the other optional metadata as well like audio, video, music and twitter metatags as well., (*17)

    $data = OpenGraph::fetch("https://unsplash.com/", true);
    

    this will give you an array like this..., (*18)

    array (
      'charset' => 'UTF8',
      'viewport' => 'width=device-width, initial-scale=1.0, maximum-scale=1.0, minimal-ui',
      'mobile-web-app-capable' => 'yes',
      'apple-mobile-web-app-capable' => 'yes',
      'apple-mobile-web-app-title' => 'Unsplash',
      'application-name' => 'Unsplash',
      'author' => 'Unsplash',
      'msapplication-config' => 'browserconfig.xml',
      'msapplication-TileColor' => '#ffffff',
      'msapplication-TileImage' => 'https://unsplash.com/mstile-144x144.png',
      'theme-color' => '#ffffff',
      'description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.',
      'twitter:site' => '@unsplash',
      'twitter:title' => 'Beautiful Free Images & Pictures | Unsplash',
      'twitter:description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.',
      'twitter:url' => 'https://unsplash.com/',
      'twitter:card' => 'summary_large_image',
      'twitter:image' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
      'title' => 'Beautiful Free Images & Pictures | Unsplash',
      'type' => 'website',
      'url' => 'https://unsplash.com/',
      'image' => 'http://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
      'image:secure_url' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9',
    )
    

    To fetch the metadata in a specific language you can pass the language as the third argument, this value will be used as the Accept-Language header., (*19)

    $url = "https://ogp.me",
    $allMeta = true, // can be false
    $language = 'en' // en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4
    $data = OpenGraph::fetch($url, $allMeta, $language);
    

    You can also pass additional Libxml parameters as the fourth argument ($options) https://www.php.net/manual/en/libxml.constants.php. Default options are set to suppress the reporting of errors and warnings, (*20)

    $data = OpenGraph::fetch($url, $allMeta, $language, $options);  
    

    You can use the fifth parameter to set the user-agent ($userAgent) of the request. The default is 'Curl'., (*21)

    $data = OpenGraph::fetch($url, $allMeta, $language, $options, $userAgent);  
    

NOTE:

Anyone having problems getting metadata from social media sites please use the following user agent set on this example:, (*22)

$opg_array = OpenGraph::fetch('URL', true, null, null, 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)');  

Exception Handling

The fetch() method, returns a FetchException with aditional data at failure., (*23)

try { $data = OpenGraph::fetch($url, true); } catch (shweshi\OpenGraph\Exceptions\FetchException $e) { $message = $e->getMessage(); $data = $e->getData(); }, (*24)

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently., (*25)

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details., (*26)

License

The MIT License (MIT). Please see License File for more information., (*27)

FOSSA Status, (*28)

Support

Buy Me A Coffee, (*29)

Happy coding!

The Versions

31/07 2018

dev-master

9999999-dev

A Laravel package to fetch website Open Graph metadata.

  Sources   Download

MIT

by Shashi Prakash Gautam

10/01 2018

1.0.1

1.0.1.0

A Laravel package to fetch website Open Graph metadata.

  Sources   Download

MIT

by Shashi Prakash Gautam

10/01 2018

1.0.0

1.0.0.0

A Laravel package to fetch website Open Graph metadata.

  Sources   Download

by Shashi Prakash Gautam