簡體   English   中英

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

我只刪除“文檔”: [{}] -> 有誰知道我刪除了這個?

檢查我的代碼:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM