PHP Post to MySQL DB in a "Custom Scripting Service" Service Type

So i’m trying to post to a MySQL DB and the post call itself seems to be working, but the body parameter doesnt seem to be accepted / working correctly. Here is my code:

//make variable for MySQL Post to Error Log table for Error Catching

$api = $platform['api'];
$post = $api->post;

$params = '{
  "resource": [
    {
      "error": "test",
      "source": "test source"
    }
  ],
  "ids": [
    0
  ],
  "filter": "string",
  "params": [
    "string"
  ]
}';

$params = json_encode($params);

$mysqlPost= $post('mysql/_table/error_log', $params);

The message I receive back is: “No record(s) detected in request.”

I believe that either formatting of the $params variable is wrong, or that doing “$post(‘mysql/_table/error_log’, $params);” is the wrong syntax for trying to post a message body to a url.

Thanks for any help!

1 Like

Try something like this. Each record is an element of the resource array. I don’t think you’ll need to include ids, filter, or params. It uses json_decode not json_encode.

$api = $platform['api'];
$post = $api->post;

// json string
$json = '{"resource":[{"error":"test", "source":"test source"}]}';
// converts json to php array
$payload = json_decode($json, true);

// makes POST call
$result = $post('mysql/_table/error_log', $payload);
1 Like

Sweet! The json_decode worked (we didn’t know about the “true” parameter in that one either)
Thank you!

Cool, glad it worked for you!