Using cUrl to post data in forms

Hello Guys,

I’m trying to send data using php cUrl to cockpit form, but nothing happening. The form stays empty.

My test code looks like
<?php
$url = ‘https://my.site/api/forms/submit/partsForm?token=MyFancyTokenKey’;
$parts = [‘start’=>‘Hard’, ‘end’=>‘Disk’];

	$ch = curl_init($url);

	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['parts' => $parts]));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

	$result = curl_exec($ch);

	curl_close($ch);

Do i need to set something in headers or different one to post data to the form?

You have to wrap your data in a form object.

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
  'form' => [
    'parts' => $parts
  ]
]));

edit: I’m not very familiar with curl… I would send a json body with the data wrapped in the form object. Not sure, if it works with just sending the raw post fields.

2 Likes

Thank you.
Everything is working fine now.