PHP code example of pear / xml_xrd

1. Go to this page and download the library: Download pear/xml_xrd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pear / xml_xrd example snippets


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


Load from string
----------------
::

    
    $myxrd = <<<XRD
    <?xml version="1.0"

     $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/');
    }

The ``<subject>`` and all ``<alias>`` tags are checked.



Link finding
============

Get all links
-------------
::

    
     $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
--------------------
Returns the first link that has the given ``relation``::

    
     $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::

    
     $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 exactly::

    
     $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
-------------------------
::

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


Properties
==========

Get a single property
---------------------
::

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


Get all properties
------------------
::

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


Get all properties of a type
----------------------------
::

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


Working with Links
==================

Accessing link attributes
-------------------------
::

    
    $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::

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


Generating XRD files
====================

.well-known/host-meta
---------------------
As described by RFC 6415::

    
     $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');
    

     $x = new XML_XRD();
    $x->subject = '[email protected]';
    
    //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');