Accessing links
Prev Next

Accessing links

The XML_XRD object offers three ways to access links:

  1. Use foreach to iterate over the object and get all links

  2. get() to fetch a single link with certain properties

  3. getAll() to fetch all links that have certain properties

The returned links are objects of type XML_XRD_Element_Link which has several properties, e.g. rel, href, type and template .

Fetching links

Get all links

<?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";
}
?>

Get link by 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";
?>

Get link by relation + optional type

If no link with the given type is found, the first link with the correct relation and an empty type will be returned

<?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";
?>

Get link by relation + type

The relation and the type both need to match exact:

<?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";
?>

Get all links by relation

<?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";
}
?>

Working with links

Accessing all link attributes

<?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;
?>

Additional link properties

Works just like properties in the XRD document.

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

Prev XML_XRD Next
Verification PEAR Manual Accessing properties