2

I can successfully do a PHP curl POST to a NodeJS API that returns a response from NodeJS

res.json({status:'ok',submission_id:id});

But I can't parse the response correctly, this returns NULL

// $newRequired = a complete array
$host = 'https://URL_HERE';
$json = json_encode($newRequired);

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json',
        'Content-Length: ' . strlen($json))
);
$response = curl_exec($ch);
$var = json_decode($response, true);
var_dump($var);

The response if I just echo $response to a page looks like this

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Sun, 27 Jan 2019 05:55:30 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 34
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
ETag: W/"XXXXXXX"

{"status":"ok","submission_id":83}

I would like to access the submission_id value from the response in PHP

Nilebac
  • 191
  • 1
  • 10

2 Answers2

2

I found the solution to my question. I had to make sure the NodeJS API wasn't going to return headers by adding this option curl_setopt($ch, CURLOPT_HEADER, false); I was able to parse the data afterwards.

Nilebac
  • 191
  • 1
  • 10
0

try to close curl before curl_close($ch);

A.Abdelhak
  • 140
  • 4