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

问题描述 投票:-1回答:2

我使用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
                        ));

                    }
php couchdb guzzle guzzlehttp couchdb-2.0
2个回答
0
投票

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

注意:如果您只想获得一个文档,也许应该添加限制为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
    ));

}

0
投票

在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
   ]
]);
© www.soinside.com 2019 - 2024. All rights reserved.