phantomjs内部奇怪地解析了POST参数 [英] POST parameters strangely parsed inside phantomjs
问题描述
我正在使用PHP/CURL,并希望通过在下面设置postfields数组将POST数据发送到我的phantomjs脚本中:
I am working with PHP/CURL and would like to send POST data to my phantomjs script, by setting the postfields array below:
在我的php控制器中,我有:
In my php controller I have:
$data=array('first' => 'John', 'last' => 'Smith');
$url='http://localhost:7788/';
$output = $this->my_model->get_data($url,$data);
在我的php模型中,我有:
In my php model I have:
public function get_data($url,$postFieldArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldArray);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
在我本地运行的phantomJS脚本中,我有:
In my phantomJS script that I am running locally I have:
// import the webserver module, and create a server
var server = require('webserver').create();
var port = require('system').env.PORT || 7788;
console.log("Start Application");
console.log("Listen port " + port);
// Create serever and listen port
server.listen(port, function(request, response) {
// Print some information Just for debbug
console.log("We got some requset !!!");
console.log("request method: ", request.method); // request.method POST or GET
if(request.method == 'POST' ){
console.log("POST params should be next: ");
console.log("POST params: ",request.post);
exit;
}
我首先从命令行启动并运行phantomjs脚本(myscript.js),然后运行php脚本.
I first start and run the phantomjs script (myscript.js) from the command line, then I run my php script.
输出为:
$ phantomjs.exe myscript.js
Start Application
Listen port 7788
null
We got some requset !!!
request method: POST
POST params should be next:
POST params: ------------------------------e70d439800f9
Content-Disposition: form-data; name="first"
John
------------------------------e70d439800f9
Content-Disposition: form-data; name="last"
Smith
------------------------------e70d439800f9--
我对输出感到困惑.我期待更多类似的东西:
I'm confused about the the output. I was expecting something more like:
first' => 'John', 'last' => 'Smith
有人可以解释为什么它看起来是这样吗?如何解析request.post对象以分配给myscript.js中的变量
Can someone explain why it looks this way? How can I parse the request.post object to assign to variables inside myscript.js
我已经做出了您在按照您的建议,我将php/curl编码更改为
As you suggested, I changed the php/curl encoding to
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postFieldArray)));
在phantomjs脚本中,我有:
In the phantomjs script, I have:
if(request.method == 'POST' ){
console.log("POST params should be next: ");
console.log(request.headers);
var data = JSON.parse(request.post);
console.log("POST params: ",data);
当我从php运行脚本时,我在控制台中看到以下内容:
when I run the script from php I see the following in my console:
Start Application
....
POST params should be next:
[object Object]
这时,脚本挂起,并且在dev tools浏览器控制台中看不到任何输出.您能建议我如何查看对象的内容吗?
At this point, the script hangs and I cannot see any output in the dev tools browser console . Can you advise me on how to see the contents of the object?
推荐答案
看起来表单已被编码为multipart/form-data
而不是application/x-www-urlencoded
.显然,当CURLOPT_POSTFIELDS
的值是一个数组时,PHP会这样做.您可以通过在调试代码中添加console.log(request.headers)
来进行检查.
It looks like the form is being encoded as multipart/form-data
rather than application/x-www-urlencoded
. Apparently PHP does this when the value of CURLOPT_POSTFIELDS
is an array. You could check this by adding console.log(request.headers)
to your debug code.
不幸的是,看起来PhantomJS不支持multipart/form-data
.如果您不愿意找到其他Web服务器,最简单的解决方案可能是使用JSON手动编码数据.我已经在我的先前的答案中修复了该错误,并添加了一些示例代码.
Unfortunately, it looks like PhantomJS doesn't support multipart/form-data
. If you're not willing to find another web server, the easiest solution is probably to manually encode the data using JSON. I've fixed the mistake in my previous answer and added some example code.
这篇关于phantomjs内部奇怪地解析了POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!