POST parameters strangely parsed inside phantomjs

Posted by user61629 on Stack Overflow See other posts from Stack Overflow or by user61629
Published on 2013-10-17T18:25:22Z Indexed on 2013/10/19 3:55 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

I am working with PHP/CURL and would like to send POST data to my phantomjs script, by setting the postfields array below:

In my php controller I have:

$data=array('first' => 'John', 'last' => 'Smith');
$url='http://localhost:7788/';
$output = $this->my_model->get_data($url,$data);

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);

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;
                }

I first start and run the phantomjs script (myscript.js) from the command line, then I run my php script.

The output is:

$ 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

Can someone explain why it looks this way? How can I parse the request.post object to assign to variables inside myscript.js

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about php