-
Notifications
You must be signed in to change notification settings - Fork 2
ObjectAPI
fkmhrk edited this page Feb 24, 2014
·
4 revisions
require_once ('<library_root>'. '/src/kii/KiiAppAPI.php');
$context = new KiiContext(APP_ID, APP_KEY, SITE);
$appPpi = new KiiAppAPI($context);
$api = $appApi->objectAPI();
$bucket = new KiiBucket($user, 'post');
$data = array(
'name' => 'fkm'
);
try {
$obj = $api->create($bucket, $data);
} catch (CloudException $e) {
//error handling
}
$obj->data['score'] = 120;
try {
$obj = $api->update($obj);
} catch (CloudException $e) {
//error handling
}
$obj is KiiObject.
This method is useful on this condition:
- You already know the object ID.
- 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
}
If the object in Kii Cloud is follows,
{
"name" : "fkm",
"score" : 100
}
$obj = new KiiObject($bucket, $id, array());
$obj->data['level'] = 'easy';
try {
$obj = $api->update($obj);
} catch (CloudException $e) {
//error handling
}
Result:
{
"level" : "easy"
}
$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"
}
$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.
try {
$api->delete($obj);
} catch (CloudException $e) {
//error handling
}
$obj is KiiObject.
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);
$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);
}