【问题标题】:POST parameters strangely parsed inside phantomjsPOST 参数在 phantomjs 中被奇怪地解析了
【发布时间】:2013-10-17 18:25:22
【问题描述】:

我正在使用 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 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 中的变量

编辑:

我已根据您在How can I send POST data to a phantomjs script 的回答中建议的更改进行了更改。

按照你的建议,我将 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]

此时,脚本挂起,我在开发工具浏览器控制台中看不到任何输出。你能告诉我如何查看对象的内容吗?

【问题讨论】:

  • 当您直接记录一个对象而不尝试将其转换为字符串时,通常会有一个箭头允许您展开和检查它——至少在 Chrome 开发人员工具中,这是我通常的利用。如果您是 Firefox 用户,也许安装 Firebug 会有所帮助。
  • 我已将 POSTMAN 扩展切换到 chrome 并取得了一些成功,请参阅 stackoverflow.com/questions/19550582/…

标签: javascript php http post phantomjs


【解决方案1】:

看起来表单被编码为multipart/form-data,而不是application/x-www-urlencoded。显然,当CURLOPT_POSTFIELDS 的值是一个数组时,PHP 会这样做。您可以通过在调试代码中添加 console.log(request.headers) 来检查这一点。

不幸的是,PhantomJS 似乎不支持multipart/form-data。如果您不愿意寻找另一个 Web 服务器,最简单的解决方案可能是使用 JSON 手动编码数据。我已经修复了previous answer 中的错误并添加了一些示例代码。

【讨论】:

  • 再次感谢您的详细回答 Sjy。请参阅上面的更新。 - 比尔
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 2012-12-13
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多