1

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

Edit:

I've made the changes you have suggested in your answer in How can I send POST data to a phantomjs script.

As you suggested, I changed the php/curl encoding to

 curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postFieldArray))); 

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

when I run the script from php I see the following in my console:

Start Application
....
POST params should be next:
[object Object]

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?

Community
  • 1
  • 1
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • When you directly log an object without trying to convert it to a string, there would normally be an arrow allowing you to expand and inspect it -- at least in Chrome developer tools, which is what I usually use. If you're a Firefox user, maybe installing Firebug would help. – sjy Oct 23 '13 at 08:44
  • I've switched over to the POSTMAN extension to chrome and have had some success please see http://stackoverflow.com/questions/19550582/parsing-post-data-in-phantomjs – user1592380 Oct 23 '13 at 19:24

1 Answers1

2

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.

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.

Community
  • 1
  • 1
sjy
  • 2,702
  • 1
  • 21
  • 22