0

So, I have an array I need to encode and send via an API.

$data = array(
            'tracking_no' => $process_list->tracking_no,       
            'status' => $status_id
        );
$data1 = (json_encode($data));

Now, I send it through curl to another site.

$curl = curl_init();

curl_setopt_array($curl, array(
          CURLOPT_URL => env('MILON_MELA_URL').'api/request/mm-process-data',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => $data1,
          CURLOPT_HTTPHEADER => array(
            'ApiAuthorization: bearer '.$responsetoken->token,
            'Content-Type: application/x-www-form-urlencoded'
          ),
));
        
$response1 = curl_exec($curl);

But for some reason, the result comes out as an array like this:

[{"tracking_no":"MM-43E9","status":"25"}]

Obviously, the function json_encode does not work here since the project thinks it is dealing with an array. So now we are stuck with a json_encoded string that thinks itself an array but cant be read. Now what? And yes, before anyone asks, I did search how to decode an array inside two brackets. It always leads me to multidimensional arrays which this is not..........

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Mash tan
  • 159
  • 11
  • Add this: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); CURLOPT_POSTFIELDS is great for data1=val1&data2=val2...... etc.. – Blazej Kita Jan 19 '23 at 09:04
  • https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl – Blazej Kita Jan 19 '23 at 09:05
  • Thanks............that took less than a second.............I really have a lot left to learn – Mash tan Jan 19 '23 at 09:06

0 Answers0