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

时间:2019-03-13 10:52:49

标签: php couchdb guzzle guzzlehttp couchdb-2.0

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

enter image description here

但是我需要这样的答复:

{
    "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
                        ));

                    }

2 个答案:

答案 0 :(得分:0)

在ouchdb中,使用 PUT 请求来编辑或添加数据,并删除删除数据

$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 :(得分:0)

您只需要修改数据结构即可。

注意:如果您只想获取一份文档,也许应该添加限制1。您还需要验证result ['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
    ));

}