dongwenghe2416 2019-01-27 06:09
浏览 318

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

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条回答 默认 最新

  • doujia1163 2019-01-27 07:12
    关注

    try to close curl before curl_close($ch);

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题