Beispiel #1
0
 function testTimestamp()
 {
     global $mlphp;
     $client = new MLPHP\RESTClient($mlphp->config['host'], 8001, 'admin', $mlphp->config['version'], $mlphp->config['username'], $mlphp->config['password'], $mlphp->config['auth'], $mlphp->config['logger']);
     $req = new MLPHP\RESTRequest('GET', 'timestamp');
     $resp = $client->send($req);
     // match a timestamp
     $this->assertStringMatchesFormat('%d-%s-%sT%d:%s:%s.%s-%s:%s', $resp->getBody());
 }
Beispiel #2
0
 function testRESTClient()
 {
     parent::$logger->debug('testRESTClient');
     $json = '{
         "namespace-bindings": [
           {
             "prefix": "will",
             "uri": "http://marklogic.com/examples/shakespeare"
           }
         ]
     }';
     $xml = '<namespace-bindings xmlns="http://marklogic.com/rest-api">
               <namespace>
                 <prefix>bill</prefix>
                 <uri>http://marklogic.com/examples/shakespeare</uri>
               </namespace>
             </namespace-bindings>';
     // PUT namespace as JSON
     $params = array('format' => 'json');
     $req = new MLPHP\RESTRequest('PUT', 'config/namespaces', $params, $json);
     $resp = self::$client->send($req);
     // GET namespaces as JSON
     $params = array('format' => 'json');
     $req = new MLPHP\RESTRequest('GET', 'config/namespaces', $params);
     $resp = self::$client->send($req);
     $this->assertEquals($resp->getBodyType(), 'json');
     $resp_parsed = json_decode($resp->getBody());
     $prefix = $resp_parsed->{'namespace-bindings'}[0]->prefix;
     $this->assertEquals($prefix, 'will');
     // GET namespaces as XML via param
     $params = array('format' => 'xml');
     $req = new MLPHP\RESTRequest('GET', 'config/namespaces', $params);
     $resp = self::$client->send($req);
     $this->assertEquals($resp->getBodyType(), 'xml');
     $dom = new \DOMDocument();
     $dom->loadXML($resp->getBody());
     $prefix = $dom->getElementsByTagName('prefix')->item(0)->nodeValue;
     $this->assertEquals($prefix, 'will');
     // GET namespaces as XML via header
     $params = array();
     $headers = array('Accept' => 'application/xml');
     $req = new MLPHP\RESTRequest('GET', 'config/namespaces', $params, '', $headers);
     $resp = self::$client->send($req);
     $this->assertEquals($resp->getBodyType(), 'xml');
     // POST additional namespace as XML
     $headers = array('Content-type' => 'application/xml');
     $req = new MLPHP\RESTRequest('POST', 'config/namespaces', $params, $xml, $headers);
     $resp = self::$client->send($req);
     // GET namespaces as XML
     $params = array('format' => 'xml');
     $req = new MLPHP\RESTRequest('GET', 'config/namespaces', $params);
     $resp = self::$client->send($req);
     $dom = new \DOMDocument();
     $dom->loadXML($resp->getBody());
     $elems = $dom->getElementsByTagName('prefix');
     $this->assertEquals(2, $elems->length);
     // DELETE namespaces
     $req = new MLPHP\RESTRequest('DELETE', 'config/namespaces');
     $resp = self::$client->send($req);
     $this->assertEquals($resp->getHttpCode(), '204');
     // HEAD via the Management API
     $manageClient = new MLPHP\RESTClient('127.0.0.1', '8001', 'admin', 'v1', 'admin', 'admin', 'digest', parent::$logger);
     $req = new MLPHP\RESTRequest('HEAD', 'timestamp');
     $resp = $manageClient->send($req);
     $this->assertEquals($resp->getHttpCode(), '200');
 }
Beispiel #3
0
// Set up global vars and class autoloading
require_once 'setup.php';
$client = $mlphp->newClient();
$client->setUsername($mlphp->config['username-admin']);
$client->setPassword($mlphp->config['password-admin']);
// Write a doc via PUT
$doc = new MLPHP\Document($client);
$file = 'example.xml';
$doc->setContentFile($file);
$uri = "/" . $file;
echo '<br />Write: ' . $doc->write($uri)->getUri() . '<br />' . PHP_EOL;
// Read a doc via GET
echo '<br />Read: ' . $doc->read($uri) . '<br />' . PHP_EOL;
// Delete a doc via DELETE
echo '<br />Delete: ' . $doc->delete($uri)->getUri() . '<br />' . PHP_EOL;
// Test 301 redirect
$govTrackClient = new MLPHP\RESTClient('www.govtrack.us', 0, 'api', 'v1');
// Get Senate bills from bill endpoint
$params = array('bill_type' => 'senate_bill');
// 'bill' resource results in redirect ('bill/' does not)
$request = new MLPHP\RESTRequest('GET', 'bill', $params);
$response = $govTrackClient->send($request);
//print_r($response->getBody());
echo '<br />Title of second bill object: <br />';
$obj = json_decode($response->getBody());
echo $obj->objects[1]->title . '<br /><br />' . PHP_EOL;
?>

</body>
</html>
Beispiel #4
0
    echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
}
// Get people named 'John' from person endpoint.
try {
    $params = array('firstname' => 'John');
    $request = new MLPHP\RESTRequest('GET', 'person', $params);
    $response = $client1->send($request);
    //print_r($response->getBody());
    echo 'Current role description of first person object: <br />';
    $obj = json_decode($response->getBody());
    echo $obj->objects[0]->current_role->description . '<br /><br />' . PHP_EOL;
} catch (Exception $e) {
    echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
}
// Client for Flickr
$client2 = new MLPHP\RESTClient('api.flickr.com', 0, 'services/rest', '');
// Get recent public photos
try {
    $params = array('method' => 'flickr.photos.getRecent', 'api_key' => 'YOUR_FLICKR_API_KEY', 'format' => 'rest');
    $request = new MLPHP\RESTRequest('GET', '', $params);
    $response = $client2->send($request);
    print_r($response->getBody());
    echo 'ID of fourth photo: <br />';
    $dom = new DOMDocument();
    $dom->loadXML($response->getBody());
    echo $dom->getElementsByTagName('photo')->item(4)->getAttribute('id') . '<br /><br />' . PHP_EOL;
} catch (Exception $e) {
    echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
}
?>