【问题标题】:GET data from Couchdb using Guzzle in php在 php 中使用 Guzzle 从 Couchdb 获取数据
【发布时间】:2019-08-04 00:38:21
【问题描述】:

我正在使用 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"
    } }

我只删除“文档”:[{}] -> 有人知道我删除了这个吗?

检查我的代码:

$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 在“docs”中返回数据:[{}]
  • 如果有任何信息,请给我链接?我检查一下谢谢

标签: php couchdb guzzle couchdb-2.0


【解决方案1】:

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

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

}

【讨论】:

    【解决方案2】:

    在 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
       ]
    ]);
    

    【讨论】:

    • 但是我使用 Guzzle 获取数据而不是使用 Curl
    • @muhammadabubakar 现在你可以用 guzzle 看到它了
    • 感谢您的回复。我在“文档”中获取数据:[{}],但我不需要这种格式,请再次检查问题
    • @muhammadabubakar 我认为这是配置错误而不是代码。请验证您的所有配置。
    猜你喜欢
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多