Javascript 在phantomjs中奇怪地解析POST参数
我正在使用PHP/CURL,希望通过设置以下postfields数组将POST数据发送到我的phantomjs脚本: 在我的php控制器中,我有:Javascript 在phantomjs中奇怪地解析POST参数,javascript,php,http,post,phantomjs,Javascript,Php,Http,Post,Phantomjs,我正在使用PHP/CURL,希望通过设置以下postfields数组将POST数据发送到我的phantomjs脚本: 在我的php控制器中,我有: $data=array('first' => 'John', 'last' => 'Smith'); $url='http://localhost:7788/'; $output = $this->my_model->get_data($url,$data); 在我的php模型中,我有: public function ge
$data=array('first' => 'John', 'last' => 'Smith');
$url='http://localhost:7788/';
$output = $this->my_model->get_data($url,$data);
在我的php模型中,我有:
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脚本中,我有:
// 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脚本
输出为:
$ 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--
我对输出感到困惑。我期待着更像:
first' => 'John', 'last' => 'Smith
有人能解释一下为什么看起来是这样吗?如何解析request.post对象以分配给myscript.js中的变量
编辑:
我已经做了您在中的答案中建议的更改
正如您所建议的,我将php/curl编码更改为
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postFieldArray)));
在phantomjs脚本中,我有:
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运行脚本时,我在控制台中看到以下内容:
Start Application
....
POST params should be next:
[object Object]
此时,脚本挂起,我在dev tools浏览器控制台中看不到任何输出。您能告诉我如何查看对象的内容吗?看起来表单被编码为
多部分/表单数据
,而不是应用程序/x-www-urlencoded
。显然,当CURLOPT_POSTFIELDS
的值是数组时,PHP会这样做。您可以通过在调试代码中添加console.log(request.headers)
来检查这一点
不幸的是,PhantomJS似乎不支持
多部分/表单数据
。如果您不想找到其他web服务器,最简单的解决方案可能是使用JSON手动编码数据。我已经修复了我的中的错误,并添加了一些示例代码。再次感谢您详细的回答Sjy。请参阅上面的更新。-Bill当您直接记录一个对象而不尝试将其转换为字符串时,通常会有一个箭头,允许您展开和检查它——至少在Chrome developer工具中是这样,这是我通常使用的。如果你是Firefox用户,也许安装Firebug会有所帮助。我已经切换到chrome的POSTMAN扩展,并取得了一些成功,请参见