Skip to content
fkmhrk edited this page Feb 24, 2014 · 4 revisions

ObjectAPI

Get ObjectAPI instance

require_once ('<library_root>'. '/src/kii/KiiAppAPI.php');

$context = new KiiContext(APP_ID, APP_KEY, SITE);
$appPpi = new KiiAppAPI($context);
$api = $appApi->objectAPI();

Create new Object

$bucket = new KiiBucket($user, 'post');
$data = array(
		'name' => 'fkm'
		);
try {
	$obj = $api->create($bucket, $data);
} catch (CloudException $e) {
	//error handling
}

Update Object

$obj->data['score'] = 120;
try {
	$obj = $api->update($obj);
} catch (CloudException $e) {
	//error handling
}

$obj is KiiObject.

Update Object Partially

This method is useful on this condition:

  1. You already know the object ID.
  2. You want to update only specified fields.
$obj = new KiiObject($bucket, $id, array());
$patch = array('level' => 'easy');
try {
	$obj = $api->updatePatch($obj, $patch);
} catch (CloudException $e) {
	//error handling
}

Difference between update and updatePatch ?

If the object in Kii Cloud is follows,

{
  "name" : "fkm",
  "score" : 100
}

Update

$obj = new KiiObject($bucket, $id, array());
$obj->data['level'] = 'easy';
try {
	$obj = $api->update($obj);
} catch (CloudException $e) {
	//error handling
}

Result:

{
  "level" : "easy"
}

UpdatePatch

$obj = new KiiObject($bucket, $id, array());
$patch = array('level' => 'easy');
try {
	$obj = $api->updatePatch($obj, $patch);
} catch (CloudException $e) {
	//error handling
}

Result:

{
  "name" : "fkm",
  "score" : 100,
  "level" : "easy"
}

Update Object If it is not modified.

$obj->data['score'] = 120;
try {
	$obj = $api->updateIfUnmodified($obj);
} catch (CloudException $e) {
	//error handling
	if ($e->getStatus() == 409) {
		echo 'Object is modified. Please fetch from cloud';
	}
}

$obj is KiiObject.

Delete Object

try {
	$api->delete($obj);
} catch (CloudException $e) {
	//error handling
}

$obj is KiiObject.

Upload Object body

We can upload 1 binary data like a tag for KiiObject. We must create a KiiObject before uploading a binary data.

$fp = fopen('myphoto.png', 'r');
try {
	$api->updateBody($obj, 'image/jpeg', $fp);
} catch (CloudException $e) {
	//error handling
}
fclose($fp);

Download Object body

$fp = fopen('downloadImage.jpg', 'w');
try {
	$api->downloadBody($obj, $fp);
} catch (CloudException $e) {
	//error handling
}
fclose($fp);

If you want to download object body on memory, please use php://memory or php://temp

$fp = fopen('php://memory', 'rw');
try {
	$api->downloadBody($obj, $fp);
	
	rewind($fp);
	// read $fp here
	fclose($fp);
} catch (CloudException $e) {
	//error handling
	fclose($fp);
}