如何通过express模块接收phpCurl发送的值却始终无法收到Node端?

2026-04-03 05:421阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计156个文字,预计阅读时间需要1分钟。

如何通过express模块接收phpCurl发送的值却始终无法收到Node端?

使用Node.js和Express开发HTTP服务时,可能会遇到request.body为空或不是标准JSON格式的情况。以下是一个简化后的解决方案:

javascriptfunction httpPost(url, data=null) { const ch=curl_init(); curl_setopt(ch, CURLOPT_URL, url); curl_setopt(ch, CURLOPT_POSTFIELDS, JSON.stringify(data));}

如何通过express模块接收phpCurl发送的值却始终无法收到Node端?

test.php

//使用node+express开发http服务时可能遇到request.body为空或不是标准json的情况 function httpPost($url, $data =null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; } //可做兼容 function httpPost2($url, $data =null, $isNode =false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if($isNode){ curl_setopt($ch, CURLOPT_HTTPHEADER, Array("content-type: application/json")); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); }else{ curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; }

本文共计156个文字,预计阅读时间需要1分钟。

如何通过express模块接收phpCurl发送的值却始终无法收到Node端?

使用Node.js和Express开发HTTP服务时,可能会遇到request.body为空或不是标准JSON格式的情况。以下是一个简化后的解决方案:

javascriptfunction httpPost(url, data=null) { const ch=curl_init(); curl_setopt(ch, CURLOPT_URL, url); curl_setopt(ch, CURLOPT_POSTFIELDS, JSON.stringify(data));}

如何通过express模块接收phpCurl发送的值却始终无法收到Node端?

test.php

//使用node+express开发http服务时可能遇到request.body为空或不是标准json的情况 function httpPost($url, $data =null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; } //可做兼容 function httpPost2($url, $data =null, $isNode =false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if($isNode){ curl_setopt($ch, CURLOPT_HTTPHEADER, Array("content-type: application/json")); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); }else{ curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output; }