0
一个小问题:ChouchDB PHP类建议
我有这个类在PHP中使用CouchDB。我发现在互联网上:
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;
}
这就是问题所在: 当我用GET方法发送功能从一个视图得到,所有文档:
$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') ;
,当我打回声$ request或var_dump($ request);返回的值是这个格式的字符串:
{"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.......
我不能得到的值(我指的是“值”:{} .....)从该字符串JSON格式。 现在的问题是:
我应该如何创建请求链接返回给我一个数组,谁在每个位置上都有一个文档? 例如:
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"}
可以这样做吗? 谢谢。
对于那些有这个问题,我解决了使用这里的所有类:[链接](http://wiki.apache.org/couchdb/Getting_started_with_PHP) – 2012-08-10 08:17:30