code-8
code-8

Reputation: 58710

How can I verify that I make a proper POST via cURL in PHP?

I have a sample json object contain these data. I'm trying to store those data in a variable, and passing through curl POST

$json = '{
    "mac": "1234567890",
    "dns": "8.8.8.8,4.2.2.1",
    "acl_mode": 1
}';

$url = 'http://my-site.com/api';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('json' => $json)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

dd($result);

My result is 500

{
    "status": 500,
    "error_code": 1005
}

Upvotes: 2

Views: 62

Answers (1)

Alex Andrei
Alex Andrei

Reputation: 7283

After testing with curl in the terminal we found out that the endpoint consumes the entire POST payload without a key.

This was used for testing

curl -X POST -d '{"mac": "1234567890","dns": "8.8.8.8,4.2.2.1","acl_mode": 1}'  http://mysite/api

So sending the $json payload as it is, without the json_encode and the array encapsulation.

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

worked just fine.

Upvotes: 1

Related Questions