尝试使用PHP从物联网获取数据

时间:2019-03-20 15:39:54

标签: php json curl iot

我正在尝试从IOT应用程序获取JSON。我可以将数据发布到我的IOT应用程序,这样我的连接就可以了,但是无法得到响应。我只会收到一个不允许的错误。

我使用以下代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);

$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$endpoint_url);
$result = curl_exec($ch);

if ($result === FALSE) {
    printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
        htmlspecialchars(curl_error($ch)));
}
curl_close($ch);

rewind($verbose);
$verboseLog = stream_get_contents($verbose);

echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";

但是我得到的只是以下内容:

Verbose information:
* Hostname integrations.thethingsnetwork.org was found in DNS cache
*   Trying 13.69.184.129...
* Connected to integrations.thethingsnetwork.org (13.69.184.129) port 443 
(#0)
* ALPN, offering http/1.1
* Cipher selection: 
ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*    subject: CN=integrations.thethingsnetwork.org
*    start date: Jan 31 12:04:08 2019 GMT
*    expire date: May  1 12:04:08 2019 GMT
*    subjectAltName: integrations.thethingsnetwork.org matched
*    issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*    SSL certificate verify ok.
> GET /ttn-eu/api/v2/down/secret/test?key=secret HTTP/1.1
Host: integrations.thethingsnetwork.org
Accept: */*

< HTTP/1.1 405 Method Not Allowed
< Server: nginx/1.13.7
< Date: Wed, 20 Mar 2019 15:33:10 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< 
* Connection #0 to host integrations.thethingsnetwork.org left intact

如何获取数据?

1 个答案:

答案 0 :(得分:1)

您没有在curl请求中指定Request类型,它会自动将其默认为GET。将以下内容添加到您的代码中。

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');