定制 fusonic/opengraph 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fusonic/opengraph

Composer 安装命令:

composer require fusonic/opengraph

包简介

PHP library for consuming and publishing Open Graph resources.

关键字:

README 文档

README

GitHub Release Packagist Downloads Packagist License GitHub Actions Workflow Status

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 metadata, 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 the 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

<?php

use Fusonic\OpenGraph\Consumer;

$consumer = new Consumer($httpClient, $httpRequestFactory);
$object = $consumer->loadUrl('https://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;           // https://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)
echo "Image[0] alt: " . $image->alt;             // The alt text

// Videos
$video = $object->videos[0];
echo "Video URL: " . $video->url;                // https://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

<?php

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 = "https://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("https://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 composer test 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/X) only publish Open Graph 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

fusonic/opengraph is licensed under the MIT license. See LICENSE for more information.

fusonic/opengraph 适用场景与选型建议

fusonic/opengraph 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 404.96k 次下载、GitHub Stars 达 104, 最近一次更新时间为 2014 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「opengraph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fusonic/opengraph 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fusonic/opengraph 我们能提供哪些服务?
定制开发 / 二次开发

基于 fusonic/opengraph 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 404.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 105
  • 点击次数: 37
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 104
  • Watchers: 6
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-19