0

我正在使用 PHP-curl 调用 REST 调用来完成官方文档中提到的设备通知消息。下面是我的代码

<?php

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://MYHUBNAME.azure-devices.net/devices/MYDVCID/messages/deviceBound/DVCTAG?api-version=2018-06-30",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "DELETE",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Authorization: MYSASTOKEN",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Host: MYHUBHOST",
    "UserAgent: Microsoft.Azure.Devices/1.17.2",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: "
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

在响应中,我总是收到以下 JSON 响应

{
  "Message": "{"errorCode":400004,"trackingId":"XXXXXXXXX-G:7-TimeStamp:XXXXXXXX","message":"BadRequest","timestampUtc":"XXXXXXXXX"}",
  "ExceptionMessage": ""
}

我尝试了很多解决它但没有得到问题,请帮助我

4

1 回答 1

0

您在请求中使用了无效的 ETAG 值。我只是使用具有有效和无效 ETAG 值的邮递员对此感到厌烦 - 使用无效 ETAG 值我看到相同的错误消息:

{
    "Message": "{\"errorCode\":400004,\"trackingId\":\"XXXXXXXXXXXXXXXXX-G:19-TimeStamp:05/17/2019 09:16:20\",\"message\":\"BadRequest\",\"timestampUtc\":\"2019-05-17T09:16:20.4954666Z\"}",
    "ExceptionMessage": ""
}

使用有效的 ETAG,我看到返回了预期的 204 状态。我从“接收设备绑定通知”GET 请求中获得的响应标头中复制了 ETAG 值。

这是我使用的邮递员文件:

{
    "info": {
        "_postman_id": "f978a27b-0ad0-4e08-a38f-2749f4cd5fcb",
        "name": "IoT Hub interactions",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "Receive Device Bound Notification",
            "request": {
                "method": "GET",
                "header": [
                    {
                        "key": "Authorization",
                        "value": "SharedAccessSignature sr=XXXXXXX",
                        "description": "Generated using VS Code extension",
                        "type": "text"
                    }
                ],
                "url": {
                    "raw": "https://MYHUB.azure-devices.net/devices/MyTestDevice/messages/deviceBound?api-version=2018-06-30",
                    "protocol": "https",
                    "host": [
                        "MYHUB",
                        "azure-devices",
                        "net"
                    ],
                    "path": [
                        "devices",
                        "MyTestDevice",
                        "messages",
                        "deviceBound"
                    ],
                    "query": [
                        {
                            "key": "api-version",
                            "value": "2018-06-30"
                        }
                    ]
                },
                "description": "See: https://docs.microsoft.com/en-us/rest/api/iothub/device/receivedeviceboundnotification"
            },
            "response": []
        },
        {
            "name": "Complete Device Bound Notification",
            "request": {
                "method": "DELETE",
                "header": [
                    {
                        "description": "Generated using VS Code extension",
                        "key": "Authorization",
                        "type": "text",
                        "value": "SharedAccessSignature sr=XXXXXXXX"
                    }
                ],
                "url": {
                    "raw": "https://MYHUB.azure-devices.net/devices/MyTestDevice/messages/deviceBound/{etag}?api-version=2018-06-30",
                    "protocol": "https",
                    "host": [
                        "MYHUB",
                        "azure-devices",
                        "net"
                    ],
                    "path": [
                        "devices",
                        "MyTestDevice",
                        "messages",
                        "deviceBound",
                        "{etag}"
                    ],
                    "query": [
                        {
                            "key": "api-version",
                            "value": "2018-06-30"
                        }
                    ]
                },
                "description": "See: https://docs.microsoft.com/en-us/rest/api/iothub/device/completedeviceboundnotification"
            },
            "response": []
        }
    ]
}
于 2019-05-17T09:24:28.867 回答