primeobsession/opengraph-io-php

dev-master 2017-09-27 17:05 UTC

This package is not auto-updated.

Last update: 2024-09-18 22:02:31 UTC


README

Opengraph-io-php 包是 opengraph.io API 的包装器,专为使用 Composer 作为包和依赖项管理的现代 PHP 项目构建。

入门指南

OpenGraph.io 的 PHP 客户端库。给定一个 URL,客户端将向 OpenGraph.io 发送 HTTP 请求,并刮取网站的 OpenGraph 标签。如果存在标签,则将标签返回给您。

很多时候,适当的标签可能不存在,这正是 OpenGraph.io 发挥作用的地方。它会推断出可能的 OpenGraph 标签,并以 hybridGraph 的形式返回给您。

hybridGraph 的结果将始终默认为页面中找到的任何 OpenGraph 标签。如果只找到一些标签或没有找到任何标签,缺失的标签将从页面内容中推断出来。

对于大多数使用情况,OpenGraph.io API 是免费的。要获取一个永久的免费密钥,请前往 OpenGraph.io 注册。

如果您最终使用量非常大,绝大多数项目将完全覆盖我们的低价格计划。根据需求,也提供专用计划。

安装

此包处于积极开发状态,无法通过 Packagist(https://packagist.org.cn)发现。但如果您现在想测试此包,您必须遵循以下步骤

  1. 在您的 PHP 项目内部克隆此存储库。由于该项目旨在作为 composer 包发布,因此您最好在项目中初始化 composer,并在项目根目录中已有一个 /vendor 目录。
    $ cd <project_root>/vendor
    $ mkdir primeobsession && cd primeobsession
    $ git clone https://github.com/primeobsession/opengraph-io-php.git
  2. 打开您的 composer.json 文件,在 "psr-4" 对象中添加以下行
    {
         ...
         "autoload": {
             ...
             "psr-4": {
                    "OpenGraph\\": "vendor/primeobsession/opengraph-io-php/src/OpenGraph"
             },
             ...
         },
         ...
    }
  3. 现在,使用以下命令重新自动加载 composer 依赖项
    $ composer dump-autoload
  4. 现在您应该能够使用正确的命名空间访问 OpenGraphClient
    use OpenGraph\OpenGraphClient;
     
    define('OG_API_KEY', 'XXXXXXXXX');
     
    $og = new OpenGraphClient(OG_API_KEY);
  • 在将此包发布到 Packagist 后,您将能够如下要求它
    $ composer require primeobsession/opengraph-io-php
    或者简单地通过在您的 composer.json 中添加以下内容
    {
        ...
        "require": {
            "php": ">=7.0.0",
            "primeobsession/opengraph-io-php": "^1.0"
        },
        ...
    }
    并更新 composer 依赖项
    $ composer update

使用方法

  • 在您的 PHP 文件顶部自动加载包,如下所示
require_once __DIR__ . '/../vendor/autoload.php'; 
  • 使用从 opengraph.io(https://opengraph.io/app/#!/account)获得的 API 密钥初始化 OpenGraph 客户端,并将其作为以下构造函数的参数传递
define('OG_API_KEY', 'XXXXXXXXX');
 
try {
    $og = new OpenGraph\OpenGraphClient(OG_API_KEY);
} catch (\OpenGraph\OpenGraphException $e) {
    echo $e->getMessage();
}
  • OpenGraph 客户端构造函数参数摘要
define('OG_API_KEY', 'XXXXXXXXX');
 
try {
    $og = new OpenGraph\OpenGraphClient(OG_API_KEY, true, true); // app_id = 'XXXXXXXXX', cache_ok = true, full_render = true
} catch (\OpenGraph\OpenGraphException $e) {
    echo $e->getMessage();
}
  • 要获取网站的 OpenGraph 响应,您必须使用 URL(要获取 og 内容的网站 URL)作为必需参数调用 fetch 方法。
define('OG_API_KEY', 'XXXXXXXXX');
 
try {
    $og = new OpenGraph\OpenGraphClient(OG_API_KEY, true, true);
    $response = $og->fetch('https://www.opengraph.io');
    echo '<pre>';
    var_dump($response);
} catch (\OpenGraph\OpenGraphException $e) {
    echo $e->getMessage();
}
  • 示例 OpenGraph 响应
object(OpenGraph\OpenGraphResponse)[4]
  protected '_id' => null
  protected '_v' => null
  protected 'url' => string 'https://opengraph.io' (length=20)
  protected 'hybridGraph' => 
    object(OpenGraph\HybridGraph)[11]
      protected 'title' => string 'OpenGraph.io - A very simple Open Graph API' (length=43)
      protected 'description' => string 'A free and simple API to retreive Open Graph data from websites, even those without it properly defined.' (length=104)
      protected 'type' => string 'website' (length=7)
      protected 'url' => string 'https://www.opengraph.io/' (length=25)
      protected 'favicon' => null
      protected 'site_name' => null
      protected 'image' => string 'http://www.opengraph.io/wp-content/uploads/2016/06/logo-simple.png' (length=66)
  protected 'openGraph' => 
    object(OpenGraph\OpenGraph)[12]
      protected 'error' => null
      protected 'title' => string 'OpenGraph.io - A very simple Open Graph API' (length=43)
      protected 'type' => string 'website' (length=7)
      protected 'admins' => null
      protected 'site_name' => string 'Opengraph.io' (length=12)
      protected 'image' => 
        object(stdClass)[8]
          public 'url' => string 'http://www.opengraph.io/wp-content/uploads/2016/06/logo-simple.png' (length=66)
      protected 'url' => string 'https://www.opengraph.io/' (length=25)
      protected 'description' => string 'A free and simple API to retreive Open Graph data from websites, even those without it properly defined.' (length=104)
  protected 'htmlInferred' => 
    object(OpenGraph\HtmlInferred)[13]
      protected 'title' => string 'OpenGraph.io - A Very Simple OpenGraph API' (length=42)
      protected 'description' => string 'Don’t waste time and resources scraping sites or trying to unfurl urls. Focus on your product and let us handle this for you!' (length=127)
      protected 'type' => string 'site' (length=4)
      protected 'url' => string 'https://opengraph.io' (length=20)
      protected 'favicon' => null
      protected 'site_name' => string 'OpenGraph.io' (length=12)
      protected 'images' => 
        array (size=2)
          0 => string 'https://www.opengraph.io/wp-content/uploads/2016/06/opengraph-white-27h.png' (length=75)
          1 => string 'http://www.opengraph.io/wp-content/uploads/2016/06/GoodData_small.png' (length=69)
      protected 'image_guess' => null
  protected 'requestInfo' => 
    object(OpenGraph\RequestInfo)[14]
      protected 'redirects' => int 0
      protected 'host' => string 'https://www.opengraph.io/' (length=25)
  protected 'accessed' => null
  protected 'updated' => 
    object(DateTime)[15]
      public 'date' => string '2017-09-22 21:03:41.501992' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=13)
  protected 'created' => 
    object(DateTime)[16]
      public 'date' => string '2017-09-22 21:03:41.502103' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=13)
  protected 'version' => string '1.1' (length=3)
  • 注意。OpenGraph 响应返回一个 OpenGraphResponse 对象的实现。如果您希望以数组的形式获取所有数据,只需在响应后追加 toArray() 方法,如下所示。
...
    var_dump($response->toArray());
...
  • 现在您将获得如下所示的数组
array (size=11)
  '_id' => string '59c52d5a1b60710023a3b82a' (length=24)
  '_v' => null
  'url' => string 'https://opengraph.io' (length=20)
  'hybridGraph' => 
    array (size=6)
      'title' => string 'OpenGraph.io - A very simple Open Graph API' (length=43)
      'description' => string 'A free and simple API to retreive Open Graph data from websites, even those without it properly defined.' (length=104)
      'type' => string 'website' (length=7)
      'url' => string 'https://www.opengraph.io/' (length=25)
      'favicon' => null
      'image' => string 'http://www.opengraph.io/wp-content/uploads/2016/06/logo-simple.png' (length=66)
  'openGraph' => 
    array (size=8)
      'error' => null
      'title' => string 'OpenGraph.io - A very simple Open Graph API' (length=43)
      'type' => string 'website' (length=7)
      'admins' => null
      'site_name' => string 'Opengraph.io' (length=12)
      'image' => 
        object(stdClass)[8]
          public 'url' => string 'http://www.opengraph.io/wp-content/uploads/2016/06/logo-simple.png' (length=66)
      'url' => string 'https://www.opengraph.io/' (length=25)
      'description' => string 'A free and simple API to retreive Open Graph data from websites, even those without it properly defined.' (length=104)
  'htmlInferred' => 
    array (size=8)
      'title' => string 'OpenGraph.io - A Very Simple OpenGraph API' (length=42)
      'description' => string 'Don’t waste time and resources scraping sites or trying to unfurl urls. Focus on your product and let us handle this for you!' (length=127)
      'type' => string 'site' (length=4)
      'url' => string 'https://opengraph.io' (length=20)
      'favicon' => null
      'site_name' => string 'OpenGraph.io' (length=12)
      'images' => 
        array (size=2)
          0 => string 'https://www.opengraph.io/wp-content/uploads/2016/06/opengraph-white-27h.png' (length=75)
          1 => string 'http://www.opengraph.io/wp-content/uploads/2016/06/GoodData_small.png' (length=69)
      'image_guess' => null
  'requestInfo' => 
    array (size=2)
      'redirects' => int 0
      'host' => string 'https://www.opengraph.io/' (length=25)
  'accessed' => int 1
  'updated' => 
    object(DateTime)[15]
      public 'date' => string '2017-09-22 15:33:46.120000' (length=26)
      public 'timezone_type' => int 2
      public 'timezone' => string 'Z' (length=1)
  'created' => 
    object(DateTime)[16]
      public 'date' => string '2017-09-22 15:33:46.120000' (length=26)
      public 'timezone_type' => int 2
      public 'timezone' => string 'Z' (length=1)
  'version' => string '1.1' (length=3)

一些示例

获取网站描述

...
    var_dump($response->hybridGraph->description);
...

获取网站徽标

...
    var_dump($response->hybridGraph->image);
...

获取网站标题

...
    var_dump($response->hybridGraph->title);
...

支持

如果您有任何问题或建议,随时可以添加到该存储库的问题中,或者如果您更愿意,可以访问 https://www.opengraph.io/support/ 并给我们留言!

许可

MIT许可

版权所有 (c) Opengraph.io

特此授予任何人无限制地使用本软件及其相关文档文件(以下简称“软件”)的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的人进行此类操作,但须遵守以下条件

上述版权声明和本许可声明应包含在软件的任何副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定目的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论该责任是源于合同、侵权或其他方式,与软件或软件的使用或其他操作有关。