Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST parameters strangely parsed inside phantomjs
    primarykey
    data
    text
    <p>I am working with PHP/CURL and would like to send POST data to my phantomjs script, by setting the postfields array below:</p> <p>In my php controller I have:</p> <pre><code>$data=array('first' =&gt; 'John', 'last' =&gt; 'Smith'); $url='http://localhost:7788/'; $output = $this-&gt;my_model-&gt;get_data($url,$data); </code></pre> <p>In my php model I have:</p> <pre><code>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); </code></pre> <p>In my phantomJS script that I am running locally I have:</p> <pre><code>// 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; } </code></pre> <p>I first start and run the phantomjs script (myscript.js) from the command line, then I run my php script.</p> <p>The output is:</p> <pre><code>$ 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-- </code></pre> <p>I'm confused about the the output. I was expecting something more like:</p> <pre><code>first' =&gt; 'John', 'last' =&gt; 'Smith </code></pre> <p>Can someone explain why it looks this way? How can I parse the request.post object to assign to variables inside myscript.js</p> <p>Edit:</p> <p>I've made the changes you have suggested in your answer in <a href="https://stackoverflow.com/questions/19418277/how-can-i-send-post-data-to-a-phantomjs-script/19418512#19418512">How can I send POST data to a phantomjs script</a>.</p> <p>As you suggested, I changed the php/curl encoding to</p> <pre><code> curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(json_encode($postFieldArray))); </code></pre> <p>In the phantomjs script, I have:</p> <pre><code>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); </code></pre> <p>when I run the script from php I see the following in my console:</p> <pre><code>Start Application .... POST params should be next: [object Object] </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload