Incorporating payload into curl in PHP

Hi everyone. My first post… How exciting!

As the title says, I’m struggling to incorporate the payload into a curl call in PHP. The following is the script and within it you can see the API Explorer-generated curl (Which works in the Explorer tool)

Below that is my curl code. This works if I remove the POSTFIELDS line (It generates a list of all my sandbox transactions) so I don’t feel to be far off - Just a syntax thing with the payload itself probably - I’m no PHP expert.

I’m stuck now and seem to have exhausted the permutations so is anybody able to help with the correct syntax for this POSTFIELDS line?

curl \
-H 'Square-Version: 2021-05-13' \
-H 'Authorization: Bearer ' \
-H 'Content-Type: application/json' \
-d '{ "idempotency_key": "67f1fd4a-23d8-48c1-8405-fe3582708dff", "source_id": "cnon:card-nonce-ok", "amount_money": { "amount": 3500, "currency": "GBP" } }'

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, "");
curl_setopt($curl,CURLOPT_POST, "true");
curl_setopt($curl,CURLOPT_HTTPHEADER, array('authorization: Bearer '.$access_token,'content-type: application/json','Square-Version: 2021-05-13','Content-Type: application/json'));

// curl_setopt($curl,CURLOPT_POSTFIELDS, ???);

$response = curl_exec($curl);
echo "$response";

Many thanks for any help.

Best regards


PS For info, node.js isn’t available with my host and I’m not allowed to install the Square SDK either unfortunately so this is my only option…

Hi @simonmarkdawson welcome to the forums!

One change, first:

  1. Remove the additional Content-Type (you have two in the headers, it will error out).

As for the data, you would want to do something like this:

$body = array(
  "idempotency_key" => "67f1fd4a-23d8-48c1-8405-fe3582708dff",
  "source_id"       => "cnon:card-nonce-ok",
  "amount_money"    => array("amount" => 3500, "currency" => "GBP")

curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($body));

Note you probably want to generate your own unique UUID rather than hardcoding it, though. This does work, though, I did test it. Let me know if you have additional questions or concerns.

1 Like

Hi SJosey

Thank you so much! I’d not been using json_encode at all and now it works!

Many thanks indeed.

Best regards


1 Like

…and point taken about the UUID


1 Like