Skip to content

Instantly share code, notes, and snippets.

@ClemensSahs
Last active December 22, 2015 05:08
Show Gist options
  • Save ClemensSahs/6421612 to your computer and use it in GitHub Desktop.
Save ClemensSahs/6421612 to your computer and use it in GitHub Desktop.
<?php
chdir(dirname(__DIR__));
require './_autoload.php';
require './_files/initDefaultDatabase.php';
use Zend\Http\Client;
use Zend\Http\Request;
define('DATABASE_URI','http://127.0.0.1:5984/klinai_test_db1/');
echo "<pre>";
$client = new Client();
/////////////////////////
// store 1st
$data = array('foo'=>'bar');
$request = new Request();
if ( isset($data['_id']) ) {
$id = $data['_id'];
unset($data['_id']);
$uri = DATABASE_URI . $id;
} else {
$id = null;
$uri = DATABASE_URI;
}
$request->setUri($uri);
$request->setMethod( ($id !== null) ? $request::METHOD_PUT : $request::METHOD_POST);
$request->setContent(json_encode($data));
$request->getHeaders()->addHeaderLine('content-type','application/json');
$response = json_decode($client->send($request)->getContent());
/////////////////////////
// get
$id = $response->id;
$request = new Request();
$request->setUri(DATABASE_URI . $id);
$request->setMethod( $request::METHOD_GET);
$request->getHeaders()->addHeaderLine('content-type','application/json');
$response = json_decode($client->send($request)->getContent());
/////////////////////////
// store 2nd
$data = array('foo'=>'bar');
$request = new Request();
if ( isset($data['_id']) ) {
$id = $data['_id'];
unset($data['_id']);
$uri = DATABASE_URI . $id;
} else {
$id = null;
$uri = DATABASE_URI;
}
$request->setUri($uri);
$request->setMethod( ($id !== null ) ? $request::METHOD_PUT : $request::METHOD_POST);
$request->setContent(json_encode($data));
$request->getHeaders()->addHeaderLine('content-type','application/json');
// if we use the toString methode only once we got no error !?!
// $request->getHeaders()->get('content-type')->toString();
$response = json_decode($client->send($request)->getContent());
if ( !property_exists($response,'error') ) {
echo 'everything ok';
exit;
}
echo "error: ";
var_dump($response);
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment