If set to application/json or text/json the object data will automatically be json_encoded upon transfer to
Riak.
public setContentType ( string $content_type ) | ||
$content_type | string |
/** * Mint a new Object instance with a json encoded string * * @param mixed $data * * @return $this */ public function buildJsonObject($data) { $this->object = new RObject(); $this->object->setData($data); $this->object->setContentType(Http::CONTENT_TYPE_JSON); return $this; }
<?php include 'bootstrap.php'; use Basho\Riak; use Basho\Riak\Api; use Basho\Riak\Command; $add = false; $name = isset($_POST['name']) ? $_POST['name'] : null; $email = isset($_POST['email']) ? $_POST['email'] : null; $id = !empty($email) ? substr(md5($email), 0, 5) : ''; if ($name && $email) { $result = (new Command\Builder\FetchObject($riak))->buildLocation($id, BUCKET_CUST, TYPE)->build()->execute(); $object = new Riak\Object(['id_s' => $id, 'email_s' => $email, 'name_s' => $name]); $object->setContentType(Api\Http::CONTENT_TYPE_JSON); if (!$result->isNotFound() && null !== $result->getObject()->getVclock()) { $object->setVclock($result->getObject()->getVclock()); } $result = (new Command\Builder\StoreObject($riak))->withObject($object)->buildLocation($id, BUCKET_CUST, TYPE)->build()->execute(); if ($result->isSuccess()) { $add = true; } } header('Location: index.php?add=' . $add);
/** * Create a new Riak object that will be stored as plain text/binary. * * @param string $key - Name of the key. * @param object $data - The data to store. * @param string $content_type - The content type of the object. (default 'application/json') * @return Object */ public function newBinary($key, $data, $content_type = 'application/json') { $obj = new Object($this->client, $this, $key); $obj->setData($data); $obj->setContentType($content_type); $obj->jsonize = false; return $obj; }