使PHP curl POST到nodeJS API但无法读取响应数据

我可以成功地对从NodeJS返回
response的NodeJS API执行PHP curl POST </ p> \ n

  res.json({status:'ok',submission_id:id}); 
</ code> </ pre>

但是我无法解析 正确响应,返回NULL </ p>

  // $ newRequired =一个完整的数组
$ 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);
</ code> </ pre>

如果我只是 echo $ response </ code>到页面的响应是这样的</ p>

  HTTP / 1.1 200 OK \  nServer:nginx  /1.10.3(Ubuntu)
日期:太阳,2019年1月27日05:55:30 GMT
内容类型: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}
</ code> </ pre>

我想从PHP中的响应中访问submission_id值</ p>
</ div>

展开原文

原文

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

2个回答



尝试在 curl_close($ ch); </ code> </ p>
</ div>之前关闭curl

展开原文

原文

try to close curl before curl_close($ch);

dongmi4809
dongmi4809 我试过这个但它仍然没有用。
一年多之前 回复



我找到了问题的解决方案。 我必须通过添加此选项 curl_setopt($ ch,CURLOPT_HEADER,false)来确保NodeJS API不会返回标题; </ code>之后我能够解析数据。</ p> \ n </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问