0

一个小问题:

我有这个类用于在 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 方法的 send 函数从一个视图中获取时,所有文档:

$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') ;

当我点击 echo $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 格式的 VALUE (我的意思是“值”:{.....})。问题是 :

我应该如何创建请求链接以返回一个数组,每个位置都有一个文档?例子 :

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"}

可以做这样的事情吗?谢谢你。

4

1 回答 1

2
$result = json_decode($result, 1);
于 2012-09-20T08:07:29.823 回答