在 php 中使用 Guzzle 从 Couchdb 获取数据

GET data from Couchdb using Guzzle in php

我正在使用 Guzzle 库将数据从 Couchdb 获取到 PHP。现在我以 POST 格式获取数据,如下所示:

但我需要这样的回复:

{
    "status": 200,
    "message": "Success",
    "device_info": {
                "_id": "00ab897bcb0c26a706afc959d35f6262",
                "_rev": "2-4bc737bdd29bb2ee386b967fc7f5aec9",
                "parent_id": "PV-409",
                "child_device_id": "2525252525",
                "app_name": "Power Clean - Antivirus & Phone Cleaner App",
                "package_name": "com.lionmobi.powerclean",
                "app_icon": "https://lh3.googleusercontent.com/uaC_9MLfMwUy6pOyqntqywd4HyniSSxmTfsiJkF2jQs9ihMyNLvsCuiOqrNxNYFq5ko=s3840",
                "last_app_used_time": "12:40:04",
                "last_app_used_date": "2019-03-12"

        "bookmark": "g1AAAABweJzLYWBgYMpgSmHgKy5JLCrJTq2MT8lPzkzJBYorGBgkJllYmiclJxkkG5klmhuYJaYlW5paphibppkZmRmB9HHA9BGlIwsAq0kecQ",
        "warning": "no matching index found, create an index to optimize query time"
    } }

我只删除了 "docs":[{}] -> 有人知道我删除了这个吗?

检查我的代码:

$response = $client->post(
                        "/child_activity_stat/_find",
                        [GuzzleHttp\RequestOptions::JSON => ['selector' => ['parent_id' => ['$eq' => $userid], 'child_device_id' => ['$eq' => $deviceid]],]]
                    );

                    if ($response->getStatusCode() == 200) {

                        $result = json_decode($response->getBody());
                        $r   = $response->getBody();



                        json_output(200, array(

                            'status'      => 200,
                            'message'     => 'Success',
                            "device_info" =>   $result
                        ));

                    }

在 couchdb 中使用 PUT 请求用于编辑或添加数据以及 DELETE 删除数据

$client = new GuzzleHttp\Client();
// Put request for edit
$client->put('http://your_url', [
  'body'            => [
     'parent_id' => ['$eq' => $userid], 
     'child_device_id' => ['$eq' => $deviceid]
  ],
  'allow_redirects' => false,
  'timeout'         => 5
]);
// To delete
$client->delete('htt://my-url', [
   'body' = [
      data
   ]
]);

你只需要修改你的数据结构。

注意:如果您只想获取一个文档,也许您应该添加 1 的限制。您还需要验证结果 ['docs'] 不为空。

示例:

<?php
$response = $client->post(
    "/child_activity_stat/_find",
    [GuzzleHttp\ RequestOptions::JSON => ['selector' => ['parent_id' => ['$eq' => $userid], 'child_device_id' => ['$eq' => $deviceid]], ]]
);

if ($response->getStatusCode() == 200) {

    // Parse as array
    $result = json_decode($response->getBody(),true);

    // Get the first document.
    $firstDoc = $result['docs'][0];

    // Remove docs from the response
    unset($result['docs']);

    //Merge sanitized $result with $deviceInfo
    $deviceInfo = array_merge_recursive($firstDoc,$result);   


    json_output(200, array(
        'status' => 200,
        'message' => 'Success',
        "device_info" => $deviceInfo
    ));

}