在phantomjs中奇怪地解析POST参数

时间:2013-10-17 18:25:22

标签: 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 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]

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

1 个答案:

答案 0 :(得分:2)

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

不幸的是,看起来PhantomJS不支持multipart/form-data。如果您不愿意找到其他Web服务器,最简单的解决方案可能是使用JSON手动编码数据。我修正了previous answer中的错误并添加了一些示例代码。