PHP does not understand JSON format so what I did is, I packed everything in array and converted it into json. The arrays of all four data are given below. In the field_mask field, I inserted directly the value of the variables.
$data1 = [
'end_device' => [
'ids' => [
'device_id' => $this->deviceId,
'dev_eui' => $this->devEui,
'join_eui' => $this->appEui
],
'join_server_address' => 'eu1.cloud.thethings.network',
'network_server_address' => 'eu1.cloud.thethings.network',
'application_server_address' => 'eu1.cloud.thethings.network',
'name' => $this->deviceId
],
'field_mask' => [
'paths' => [
'eu1.cloud.thethings.network',
'eu1.cloud.thethings.network',
'eu1.cloud.thethings.network',
$this->devEui,
$this->appEui,
$this->deviceId
],
],
];
$data2 = [
'end_device' => [
'ids' => [
'device_id' => $this->deviceId,
'dev_eui' => $this->devEui,
'join_eui' => $this->appEui
],
'network_server_address' => 'eu1.cloud.thethings.network',
'application_server_address' => 'eu1.cloud.thethings.network',
'network_server_kek_label' => '',
'application_server_kek_label' => '',
'application_server_id' => '',
'net_id' => Null,
'root_keys' => [
'app_key' => [
'key' => $this->appKey
],
],
],
'field_mask' => [
'paths' => [
$this->deviceId,
$this->devEui,
$this->appEui,
'',
'',
'',
Null,
$this->appKey
],
],
];
$data3 = [
'end_device' => [
'multicate' => False,
'supports_join' => True,
'lorawan_version' => $this->loraVersion,
'ids' => [
'device_id' => $this->deviceId,
'dev_eui' => $this->devEui,
'join_eui' => $this->appEui
],
'mac_settings' => [
'supports_32_bit_f_cnt' => True
],
'supports_class_c' => False,
'supports_class_b' => False,
'lorawan_phy_version' => $this->phyVersion,
'frequency_plan_id' => $this->frequencyPlan,
],
'field_mask' => [
'path' => [
False,
True,
$this->loraVersion,
$this->deviceId,
$this->devEui,
$this->appEui,
True,
False,
False,
$this->phyVersion,
$this->frequencyPlan
],
],
];
$data4 = [
'end_device' => [
'ids' => [
'device_id' => $this->deviceId,
'dev_eui' => $this->devEui,
'join_eui' => $this->appEui
],
],
'field_mask' => [
'paths' => [
$this->deviceId,
$this->devEui,
$this->appEui
],
],
];
I converted all of them into JSON using json_encode()
$data_end_device= json_encode($data1);
$data_join_server = json_encode($data2);
$data_network_server = json_encode($data3);
$data_application_server = json_encode($data4);
I am using bearer authentication to make the PHP Post request.
$url1 = 'https://eu1.cloud.thethings.network/api/v3/applications/'.$this->appId.'/devices';
$url2 = 'https://eu1.cloud.thethings.network/api/v3/js/applications/'.$this->appId.'/devices';
$url3 = 'https://eu1.cloud.thethings.network/api/v3/ns/applications/'.$this->appId.'/devices';
$url4 = 'https://eu1.cloud.thethings.network/api/v3/as/applications/'.$this->appId.'/devices';
$app_api = Application::where('app_id', $this->appId)->first()->api_key;
$authorization = "Authorization: Bearer " . $app_api;
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array($authorization, 'Content-Type: application/json'));
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_end_device);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
$result1 = curl_exec($ch1);
curl_close($ch1);
$ch2 = curl_init($url2);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array($authorization, 'Content-Type: application/json'));
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data_join_server);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);
$result2 = curl_exec($ch2);
curl_close($ch2);
$ch3 = curl_init($url3);
curl_setopt($ch3, CURLOPT_HTTPHEADER, array($authorization, 'Content-Type: application/json'));
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch3, CURLOPT_POSTFIELDS, $data_network_server);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 1);
$result3 = curl_exec($ch3);
curl_close($ch3);
$ch4 = curl_init($url4);
curl_setopt($ch4, CURLOPT_HTTPHEADER, array($authorization, 'Content-Type: application/json'));
curl_setopt($ch4, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch4, CURLOPT_POSTFIELDS, $data_application_server);
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch4, CURLOPT_FOLLOWLOCATION, 1);
$result4 = curl_exec($ch4);
curl_close($ch4);