node使用了express4和body-parser来解析php curl的数据,但是获取的req.body是{},设置了header
前提知识:
body-parser不支持解析multi/form-data的功能,如果是传递二进制数据或者文件上传,就不能用它了。
Node.js body parsing middleware.
This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:
busboy and connect-busboy
multiparty and connect-multiparty
formidable
multer
This module provides the following parsers:
JSON body parser
Raw body parser
Text body parser
URL-encoded form body parser
参考:
https://github.com/expressjs/body-parser#bodyparserurlencodedoptions
PHP 代码
Java代码
function addCurl($url, $type="get", $postData=null) { $ch = curl_init(); $headers[] = 'Connection: Keep-Alive'; $headres[] = 'Content-Type: application/x-www-form-urlencoded;charset=utf-8'; $headers[] = 'Content-Length: ' . strlen(json_encode($postData)); //$headres[] = 'Content-Type: application/json'; //$headres[] = 'Content-Type: text/html'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($type=="get") { curl_setopt($ch, CURLOPT_POST, 0); } else { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postData))); } curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch) . PHP_EOL; curl_close($ch); return false; } curl_close($ch); return ($code == 200) ? $data : "server error,code: . {$code}"; }
node 代码
Java代码
var express = require('express') var bodyParser = require('body-parser') var app = express() var urlencodedParser = bodyParser.urlencoded({ extended: false }) app.use(urlencodedParser, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('you posted:\n') res.end(JSON.stringify(req.body, null, 2)) }) app.listen(3000, function() { console.log('Server is running') })
指定了解析方式依然不行。就到body-parser的源码中一行行调试下去。
找到urlencode.js
Java代码
// determine if request should be parsed if (!shouldParse(req)) { return ('skip parsing'), next() }
这行shouldParse 返回false
再到type-is.js/index.js 返回false。
var value = req.headers['content-type']
Java代码
function typeofrequest(req, types_) { var types = types_ // no body if (!hasbody(req)) { return null } // support flattened arguments if (arguments.length > 2) { types = new Array(arguments.length - 1) for (var i = 0; i < types.length; i++) { types[i] = arguments[i + 1] } } // request content type var value = req.headers['content-type'] return typeis(value, types) }
发现这里的req.headers['content-type'] 是 multipart/form-data,而type是application/x-www-form-urlencoded
原来,在php执行curl的时候,postData是数组,会将数据编码设置为 multipart/form-data
Java代码
Note:
传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded。
修改pHP代码
Java代码
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postData)));

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,数据库imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)优化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依赖性注射inphpisadesignpatternthatenhancesFlexibility,可检验性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能优化可以通过以下步骤实现:1)在脚本顶部使用require_once或include_once减少文件加载次数;2)使用预处理语句和批处理减少数据库查询次数;3)配置OPcache进行opcode缓存;4)启用并配置PHP-FPM优化进程管理;5)使用CDN分发静态资源;6)使用Xdebug或Blackfire进行代码性能分析;7)选择高效的数据结构如数组;8)编写模块化代码以优化执行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

记事本++7.3.1
好用且免费的代码编辑器