[英]trying to do curl post request using php
I know this isn't new, using php to do a post request using curl, but I am battling with this one.我知道这不是新的,使用 php 使用 curl 进行发布请求,但我正在与这个做斗争。 The curl command line input which does work sucessfully is as follows:成功运行的 curl 命令行输入如下:
curl -v -X POST --data "{\"dev_id\":\"a8610a3237397a01\",\"payload_raw\":\"Ag==\"}" https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16-xxxxxxxxxxxxxxxxxxxxxx
And I am trying to do the equivalent in PHP so as to be able to action a downlink to my endpoint in things network.我正在尝试在 PHP 中做同样的事情,以便能够在事物网络中对我的端点执行下行链路。 I am trying the following:我正在尝试以下操作:
<?php
echo('hello');
$url = 'https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16--xxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'AQ=='
);
$payload = json_encode(array("payload_raw" => 'AQ=='));
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$output=curl_exec($ch);
$info = curl_getinfo($ch);
echo "<pre>";
print_r($info);
echo "</pre>";
// Close cURL resource
curl_close($ch);
?>
The result I get is:我得到的结果是:
hello
Array
(
[url] => https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16--xxxxxxxxxxxxxxxxxxxxxx
[content_type] => text/plain; charset=utf-8
[http_code] => 400
[header_size] => 171
[request_size] => 270
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.203
[namelookup_time] => 1.0E-6
[connect_time] => 1.0E-6
[pretransfer_time] => 1.0E-6
[size_upload] => 22
[size_download] => 0
[speed_download] => 0
[speed_upload] => 108
[download_content_length] => 0
[upload_content_length] => 22
[starttransfer_time] => 0.203
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 13.69.184.129
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 10.0.0.109
[local_port] => 53234
)
so it isn't happy with my data.所以它对我的数据不满意。 I'm just trying to send a payload_raw in base64 encoding of the value: 'AQ==' Any ideas anyone?我只是想在值的base64编码中发送一个payload_raw:'AQ=='有什么想法吗?
Ok, so I forgot to include he device ID as part of the payload.好的,所以我忘了将他的设备 ID 作为有效负载的一部分。 Solved.解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.