Accessing REST from PHP

Example of using curl to issue an authenticated REST request.

<?php

    // Demonstrates two methods of including authorisation
    //      for accessing a resource
    // The resource in this case is an xml export of the account
    //      table of document Acme.moneyworks

    if($_GET["auth"] == "headers")  {
        $ch = curl_init();
        // set URL and other appropriate options
        curl_setopt($chCURLOPT_URL"http://127.0.0.1:6710/REST/Acme.moneyworks/
                                        export/table=account&format=xml-terse");
        curl_setopt($chCURLOPT_HEADER0);
        curl_setopt($chCURLOPT_RETURNTRANSFER1);
        // set Auth headers for Datacentre login, and document login
        $headers = array(
            "Authorization: Basic " . base64_encode("root:Datacentre:XXXX"),
            "Authorization: Basic " . base64_encode("Admin:Document:fred"));
        curl_setopt($chCURLOPT_HTTPHEADER$headers); 
        $result = curl_exec($ch);
        curl_close($ch);
        }
    else if($_GET["auth"] == "inline")  {
        $ch = curl_init();
        // usernames and passwords are inline in the URL
        // ask for verbose output so we can see the difference
        curl_setopt($chCURLOPT_URL"http://root:XXXX@127.0.0.1:6710/
                                      REST/Admin:fred@Acme.mwd6/export/
                                      table=account&format=xml-verbose");
        curl_setopt($chCURLOPT_HEADER0);
        curl_setopt($chCURLOPT_RETURNTRANSFER1);
        $result = curl_exec($ch);
        curl_close($ch);
        }

    header("Content-Type: text/xml");
    echo $result;   
    ?>
Posted in REST | Comments Off on Accessing REST from PHP