One little problem:
I have this class for working with CouchDB in PHP. I found it on the internet:
class CouchSimple {
public $user = "admin";
public $pass = "admin";
function CouchSimple($options) {
foreach($options AS $key => $value) {
$this->$key = $value;
}
}
function send($method, $url, $post_data = NULL) {
$s = fsockopen($this->host, $this->port, $errno, $errstr);
if(!$s) {
echo "$errno: $errstr\n";
return false;
}
$request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
if ($this->user) {
$request .= "Authorization: Basic ".base64_encode("$this->user:$this->pass")."\r\n";
}
if($post_data) {
$request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
$request .= "$post_data\r\n";
}
else {
$request .= "\r\n";
}
fwrite($s, $request);
$response = "";
while(!feof($s)) {
$response .= fgets($s);
}
list($this->headers, $this->body) = explode("\r\n\r\n", $response);
return $this->body;
}
This is the problem :
When I use the send function with GET method for get from one view, all docs :
$options ['host'] = "localhost" ;
$options ['port'] = "5984" ;
$couchdb = new CouchSimple($options) ;
$couchdb -> user = "admin" ;
$couchdb -> pass = "admin" ;
$request = $couchdb -> send ("GET", '/verge/_design/application/_view/posts_by_user') ;
and when I hit echo $request or var_dump($request) ; the returned value is a string with this format :
{"total_rows":8,"offset":0,"rows":[ {"id":"ce9c512257acf399b40edc10e40006d1","key":"ce9c512257acf399b40edc10e40006d1","value":{"_id":"ce9c512257acf399b40edc10e40006d1","_rev":"1-55caf5929e19c7ee1c855baeb9bc1f81","date_created":"Thu, 09 Aug 2012 14:39:04 +0300","content":"asdsada","user":"123","type":"post"},"doc":{"_id":"ce9c512257acf399b40edc10e40006d1","_rev":"1-55caf5929e19c7ee1c855baeb9bc1f81","date_created":"Thu, 09 Aug 2012 14:39:04 +0300","content":"asdsada","user":"123","type":"post"}}, {"id":"ce9c512257acf399b40edc10e4000e35","key":"ce9c512257acf399b40edc10e4000e35","value":{"_id":"ce9c5.......
And I can’t get the VALUE ( I mean the “value”:{…..} ) with JSON format from this string.
The question is :
How should I create the request link to return me an array who on each position have a document?
Example :
echo $result [0] ; // {"_id":"123o1josjg9023","_rev":"3-asfdksngosdnhon123o5234","date_created":"Thu, 09 Aug 2012 14:39:04" +0300","content":"kggafasf","user":"123","type":"post"}
echo $result [1] ; // {"_id":"123o1josjg9asdg023","_rev":"1-asfdksngosdnhon123gsgo5234","date_created":"Thu, 09 Aug 2012 14:33:04" +0300","content":"asdasgaa","user":"123","type":"post"}
Can do something like this?
Thank you.
1 Answer