2013-10-17 58 views
1

我用PHP /捲曲的工作,並希望POST數據發送到我的phantomjs腳本,通過設置以下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_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] 

此時,腳本掛起,在開發工具bro中看不到任何輸出wser控制檯。你能告訴我如何看到對象的內容嗎?

+0

當您直接記錄對象而不嘗試將其轉換爲字符串時,通常會出現一個箭頭,允許您展開並檢查它 - 至少在Chrome開發人員工具中,這是我通常使用的。如果你是Firefox用戶,安裝Firebug可能會有所幫助。 – sjy

+0

我已經切換到鉻的POSTMAN擴展,並已取得了一些成功,請參閱http://stackoverflow.com/questions/19550582/parsing-post-data-in-phantomjs – user61629

回答

2

它看起來像表格編碼爲multipart/form-data而不是​​。顯然,當CURLOPT_POSTFIELDS的值是一個數組時,PHP會執行此操作。您可以通過將console.log(request.headers)添加到您的調試代碼來進行檢查。

不幸的是,它看起來像PhantomJS不支持multipart/form-data。如果您不想找到其他Web服務器,最簡單的解決方案可能是使用JSON手動編碼數據。我已經修復了我的previous answer中的錯誤並添加了一些示例代碼。

+0

再次感謝您的詳細解答Sjy。請參閱上面的更新。 - 比爾 – user61629