- 浏览: 1077667 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (290)
- php (65)
- javascript (36)
- html5 (31)
- thinkphp (9)
- mysql (16)
- jquery (13)
- node.js (9)
- css (9)
- android 开发 (8)
- flex (5)
- java (3)
- apache (8)
- linux (8)
- git (5)
- web (5)
- wordpress (9)
- mongodb (2)
- redis (5)
- yaf (6)
- python (4)
- big data (1)
- sphinx (1)
- html (1)
- bootstrap (1)
- vue (1)
- laravel (1)
- test (0)
最新评论
-
July01:
推荐用StratoIO打印控件,支持网页、URL、图片、PD、 ...
如何解决非IE浏览器的web打印 -
flashbehappy:
同一个视频,有mp4,ogg两种格式的。在chrome,fir ...
firefox chrom safari 对video标签的区别 -
xmdxzyf:
可以在网站(www.sosoapi.com)上试下在线表单方式 ...
用swagger-php/ui做API测试 -
flex_莫冲:
a2631500 写道"看了源码,设置Backbon ...
backbone与php交互 -
a2631500:
"看了源码,设置Backbone.emulateJS ...
backbone与php交互
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 代码
node 代码
指定了解析方式依然不行。就到body-parser的源码中一行行调试下去。
找到urlencode.js
这行shouldParse 返回false
再到type-is.js/index.js 返回false。
var value = req.headers['content-type']
发现这里的req.headers['content-type'] 是 multipart/form-data,而type是application/x-www-form-urlencoded
原来,在php执行curl的时候,postData是数组,会将数据编码设置为 multipart/form-data
修改pHP代码
参考:
http://php.net/manual/zh/function.curl-setopt.php
前提知识:
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 代码
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 代码
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
// 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']
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
Note: 传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded。
修改pHP代码
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postData)));
参考:
http://php.net/manual/zh/function.curl-setopt.php
发表评论
-
将博客搬至CSDN
2017-03-28 09:07 501将博客搬至CSDN,尽情期待 -
laravel入門
2017-03-03 16:31 5501 全局安裝 composer global require ... -
导出csv,excel等文件,文件内容错误的问题,需要加上ob_end_clean
2017-03-02 11:54 1465见这个问题: http://stackoverflow.com ... -
facebook graph api从2.6转到2.8的问题处理
2017-02-15 16:30 943之前一直用facebook 2.6的api,现在想升级到2.8 ... -
php从mysql读取超过200W行的表
2017-01-04 10:09 557需要从一个大表,如user表中读取所有user数据再做整理并導 ... -
一个session失效的伪命题
2016-12-08 18:33 660最近遇到一个问题,一个ajax请求会调用已存在的session ... -
tp5与tp3的区别
2016-11-21 14:33 1482TP5作了很大的改变,更加灵活,没那么臃肿了,加入了一些令人振 ... -
推送消息能不能区分禁止通知和卸载两种类型?
2016-11-18 10:54 1367消息推送ios用了apns,android用的是gcm。推送失 ... -
yaf在命令行模式下出现PHP Fatal error: Class 'Yaf_Application' not found的解决办法
2016-11-11 16:42 2313为了跑个yaf的crontab,执行时报错: PHP Fata ... -
推送emoji表情乱码的解决方法
2016-11-04 17:44 1891推送内容如果包含了emoji表情,需要做以下修改 1 mysq ... -
YAF访问图片等静态资源禁止调用YAF框架的解决办法
2016-10-20 17:55 1571当访问yafpublic 目录下的某个静态资源时,依然会访问y ... -
在线自动协同编辑文本的实现方案
2016-10-14 16:49 1915要实现可以自动在线协同编辑文本的功能。就类似google do ... -
Paypal支付跳转失败的原因及解决办法
2016-09-18 14:27 3515遇到在跳转到paypal支付页面的时候出现502 bad ga ... -
文件格式不是unix导致sed,cat等读取文件后无法正常退出
2016-08-30 14:44 713#!/bin/bash bucket_config=&quo ... -
aws sdk之nodejs s3
2016-08-19 12:19 2834upload 和putObject有什么区别,同样是上传或新增 ... -
varnish的使用和PHP清除缓存的技巧
2016-08-11 14:10 1226官網地址 https://www.varnish-cache. ... -
TP在APP_DEBUG=false的情況下,CLI和WEBSITE會共用同一個~runtime.php導致出錯的問題解決辦法
2015-12-14 11:43 782TP若使用了 define('APP_DEBU ... -
winston log split by date的解决方案
2015-11-25 11:56 1305winston 是node.js的日志管理工具,但是它的api ... -
nodejs程序后台一直运行
2015-10-22 09:57 2831来源:http://materliu.github ... -
解决nodemailer发送email提示Invalid login 的问题
2015-09-24 10:57 1765上代码 var nodemailer = require ...
相关推荐
书由浅入深,全面、系统地介绍了Node.js开发...Node.js的日志模块、curl模块、crontab模块、forever模块、xml模块和邮件发送模块等应用工具。 本书非常适合从事编程开发的学生、教师、广大科研人员和工程技术人员研读。
nodebrew, node.js 版本管理器 nodebrew Node.js 版本管理器。安装用curl安装。$ curl -L git.io/nodebrew | perl - setup或者,下载和设置。$ wget git.io/nodebrew
PHP100视频教程88:PHP.之.CURL模拟登陆并获取数据.rar
用来替换WAMP server下的php_curl.dll,解决加载curl报错或无法加载问题。下载解压后,覆盖wamp\bin\php\php5.4.3\ext目录下对应文件,重启apache即可。如果不行请检查: 是否已修改php.ini文件去掉extension=...
速度测试服务器用 Node.js 编写的 curl 速度测试服务器我如何使用它? 只需运行npm install和node index.js或使用提供的 Dockerfile。 要测试下载 10 兆字节的数据,请运行curl localhost:5000/megabytes/10 。 您还...
与nvm exec不同,它可以:一次运行多个Node.js版本,可以以编程方式运行,速度提高5倍,不需要为每个Node版本单独执行安装步骤。在特定的Node.js版本上运行任何命令。 与nvm exec不同,它可以:一次运行多个Node.js...
PHP100视频教程87:PHP.之.CURL.传输与获取功能.rar
一个将 curl 命令转为 Python、Node.js、R、PHP、Go 代码的工具
n-install:安装n,即Node.js版本管理器,而无需先安装Node.js:curl -L https:git.ion-install | 重击
REST API 的身份验证模块,使用 Node.js 和 Express.js 框架以及 Mongoose.js 来处理 MongoDB。 对于访问控制,该项目在 OAuth2orize 和 Passport.js 的帮助下使用 OAuth 2.0。 此代码基于和文章。 运行项目 你需要...
主要介绍了php的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll,...
qt工程中引入编译好的curl库,封装了一个类来执行post请求 可以直接拿来改造。 数据格式为json格式
curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and...
hp的curl封装类用法,以实例形式较为详细的讲述了curl封装类及其使用方法,并总结了GET与POST的用法。 使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll) 开启php ...
graphqurl是一个类似于curl用于GraphQL的CLI
Snap7 Express服务器一个简单的node.js表示REST服务器即可写入siemens徽标。用法一个端点打开大门。 $ curl --location --request GET ' ...
c++ 发送https POST请求并获取返回数据(包含appKey与appSecret)
windows下C开发CURL的Demo
httpstat一个类似于curl的工具,能够可视化http / https过程并显示持续时间。
实践博客Node.js 实践API 文档根: 路由(使用 x-www-form-urlencoded (--data 在 curl -X 末尾)):帖子发布(创建): 获取(阅读): 放置(更新): 删除:注释发布(创建): 获取(阅读): 放置(更新): ...