pear/xml_xrd

PHP 库,用于解析和生成 "可扩展资源描述符 (XRD) 版本 1.0" 文件

v0.3.1 2014-07-17 20:50 UTC

This package is auto-updated.

Last update: 2024-09-16 18:33:14 UTC


README

PHP 库,用于解析和生成 可扩展资源描述符 (XRD) 版本 1.0 文件。它支持加载和保存 XML (XRD) 和 JSON (JRD) 标记。

XRD 和 JRD 文件用于 .well-known/host-meta 文件,如 RFC 6415: 网络主机元数据 中标准化所述。

基于 JRD 的 Webfinger 可用于通过电子邮件地址发现用户信息,例如他们的 OpenID 提供者 URL。

XRD 格式取代了 XRI 2.0 中定义的 XRDS 格式,该格式用于 Yadis 通信协议

内容

示例

加载 XRD 文件

从文件加载

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
try {
    $xrd->loadFile('/path/to/my.xrd', 'xml');
} catch (XML_XRD_Exception $e) {
    die('Loading XRD file failed: '  . $e->getMessage());
}

从字符串加载

<?php
$myxrd = <<<XRD
<?xml version="1.0"?>
<XRD>
 ...
XRD;

require_once 'XML/XRD.php';
$xrd = new XML_XRD();
try {
    $xrd->loadString($myxrd, 'xml');
} catch (XML_XRD_Exception $e) {
    die('Loading XRD string failed: '  . $e->getMessage());
}

验证

验证主题

检查 XRD 文件是否真正描述了请求 XRD 的资源/URL

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
if (!$xrd->describes('http://example.org/')) {
    die('XRD document is not the correct one for http://example.org/');
}

检查 <subject> 和所有 <alias> 标签。

查找链接

获取所有链接

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
foreach ($xrd as $link) {
    echo $link->rel . ': ' . $link->href . "\n";
}

按关系获取链接

返回第一个具有给定 relation 的链接

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
$idpLink = $xrd->get('lrdd');
echo $idpLink->rel . ': ' . $idpLink->href . "\n";

按关系 + 可选类型获取链接

如果找不到具有给定 type 的链接,则返回第一个具有正确 relation 和空 type 的链接

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
$link = $xrd->get('lrdd', 'application/xrd+xml');
echo $link->rel . ': ' . $link->href . "\n";

按关系 + 类型获取链接

需要精确匹配 relationtype

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
$link = $xrd->get('lrdd', 'application/xrd+xml', false);
echo $link->rel . ': ' . $link->href . "\n";

按关系获取所有链接

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
foreach ($xrd->getAll('lrdd') as $link) {
    echo $link->rel . ': ' . $link->href . "\n";
}

属性

获取单个属性

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
if (isset($xrd['http://spec.example.net/type/person'])) {
    echo $xrd['http://spec.example.net/type/person'] . "\n";
}

获取所有属性

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
foreach ($xrd->getProperties() as $property) {
    echo $property->type . ': ' . $property->value . "\n";
}

获取所有类型的属性

<?php
require_once 'XML/XRD.php';
$xrd = new XML_XRD();
$xrd->loadFile('http://example.org/.well-known/host-meta');
foreach ($xrd->getProperties('http://spec.example.net/type/person') as $property) {
    echo $property->type . ': ' . $property->value . "\n";
}

处理链接

访问链接属性

<?php
$link = $xrd->get('http://specs.openid.net/auth/2.0/provider');

$title = $link->getTitle('de');
$url   = $link->href;
$urlTemplate = $link->template;
$mimetype    = $link->type;

附加链接属性

就像 XRD 文档中的属性一样工作

<?php
$link = $xrd->get('http://specs.openid.net/auth/2.0/provider');
$prop = $link['foo'];

生成 XRD 文件

.well-known/host-meta

如 RFC 6415 中所述

<?php
require_once 'XML/XRD.php';
$x = new XML_XRD();
$x->subject = 'example.org';
$x->aliases[] = 'example.com';
$x->links[] = new XML_XRD_Element_Link(
    'lrdd', 'http://example.org/gen-lrdd.php?a={uri}',
    'application/xrd+xml', true
);
echo $x->to('xml');
?>

如果您想要 JSON 格式的 JRD

echo $x->to('json');

Webfinger 文件

<?php
require_once 'XML/XRD.php';
$x = new XML_XRD();
$x->subject = 'user@example.org';

//add link to the user's OpenID
$x->links[] = new XML_XRD_Element_Link(
    'http://specs.openid.net/auth/2.0/provider',
    'http://id.example.org/user'
);
//add link to user's home page
$x->links[] = new XML_XRD_Element_Link(
    'http://xmlns.com/foaf/0.1/homepage',
    'http://example.org/~user/'
);

echo $x->to('jrd');
?>

错误处理

在加载文件时,可能会抛出类型为 XML_XRD_Exception 的异常。代码的其他部分不会抛出异常,而是优雅地失败并返回 null,例如,当属性不存在时。

使用 loadFile() 可能会导致 PHP 警告,如下所示

Warning: simplexml_load_file(https://example.org/) failed to open stream: Connection refused

这无法正确防止,因此您要么使用 @ 静音它,要么手动获取文件并使用 loadString()

待办事项

  • XML 签名验证
  • (非常可选) XRDS (多个 XRD)?

链接