'php curl & ThingsPeak

Can anyone help me ? - I have no idea what I'm doing wrong.... The normal upload with only one record works fine, but I can't get the bulk_upload in the json-format to work. This is the code (channelID and Key changed) - but no data arrives at ThingsPeak!

<?php
$age = '{
    "write_api_key": "XJZRU8Z8FMW",
    "updates": [{
            "created_at": "2022-01-30 10:26:2 -0500",
            "field1": 100
        },
        {
            "created_at": "2022-01-30 10:26:23 -0500",
            "field1": "red",
            "field2": "blue",
            "field3": "green",
            "field4": "fish",
            "field5": "alimony",
            "field6": 100,
            "field7": 100,
            "field8": 100,
            "status": "good"
        },
        {
            "created_at": "2022-01-30 10:26:26 -0500",
            "field1": 1500,
            "field2": 150,
            "field3": 100,
            "field4": 100,
            "field5": 100,
            "field6": 100,
            "field7": 100,
            "field8": 100,
            "latitude": 123,
            "longitude": 23,
            "elevation": 34
        }
    ]
}';
$content = json_encode($age);
$url = "https://api.thingspeak.com/channels/17253/bulk_update.json";    
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
?>


Solution 1:[1]

there's 2 issues here,

issue #1: you're double-json-encoding the post data, replace

$age = '{
    "write_api_key": "XJZRU8Z8FMW",
    "updates": [{
            "created_at": "2022-01-30 10:26:2 -0500",
            "field1": 100
        },
        {
            "created_at": "2022-01-30 10:26:23 -0500",
            "field1": "red",
            "field2": "blue",
            "field3": "green",
            "field4": "fish",
            "field5": "alimony",
            "field6": 100,
            "field7": 100,
            "field8": 100,
            "status": "good"
        },
        {
            "created_at": "2022-01-30 10:26:26 -0500",
            "field1": 1500,
            "field2": 150,
            "field3": 100,
            "field4": 100,
            "field5": 100,
            "field6": 100,
            "field7": 100,
            "field8": 100,
            "latitude": 123,
            "longitude": 23,
            "elevation": 34
        }
    ]
}';

with

$age = array (
  'write_api_key' => 'XJZRU8Z8FMW',
  'updates' => 
  array (
    array (
      'created_at' => '2022-01-30 10:26:2 -0500',
      'field1' => 100,
    ),
    array (
      'created_at' => '2022-01-30 10:26:23 -0500',
      'field1' => 'red',
      'field2' => 'blue',
      'field3' => 'green',
      'field4' => 'fish',
      'field5' => 'alimony',
      'field6' => 100,
      'field7' => 100,
      'field8' => 100,
      'status' => 'good',
    ),
    array (
      'created_at' => '2022-01-30 10:26:26 -0500',
      'field1' => 1500,
      'field2' => 150,
      'field3' => 100,
      'field4' => 100,
      'field5' => 100,
      'field6' => 100,
      'field7' => 100,
      'field8' => 100,
      'latitude' => 123,
      'longitude' => 23,
      'elevation' => 34,
    ),
  ),
);

issue #2: either channel 17253 does not exist, -or- api key XJZRU8Z8FMW is not a valid key for channel 17253.

and protip, next time you want help with some api, add a link to the relevant api documentation in your question, in this case https://www.mathworks.com/help/thingspeak/bulkwritejsondata.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1