在phantomjs中获取POST数据作为对象?

时间:2013-07-18 23:32:14

标签: php javascript curl phantomjs

我在this issue做了一些阅读,我发现,在某一点上,我想要的是可能的。在评论#3中,显示:

request.post = {
  Name : "Jonathan Doe",
  Age : "23",
  Formula : "a + b == 13%!"
}

现在,当我向我的PhantomJS网络服务器发送POST请求时,这正是我想要的。

我这样发送:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "localhost:8585");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
 'ajaxUrl' => $ajaxUrl,
 'analysisFile' => $analysisFile,
 'businessId' => $businessId,
 'website' => $website
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); 

..但我得到的请求看起来像这样(在JSON.stringify之后):

{ "headers" : { "Accept" : "*/*",
      "Content-Length" : "555",
      "Content-Type" : "multipart/form-data; boundary=----------------------------ad33c9f28b99",
      "Expect" : "100-continue",
      "Host" : "localhost:8585"
    },
  "httpVersion" : "1.1",
  "method" : "POST",
  "post" : "------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"ajaxUrl\"\r\n\r\nhttp://localhost/website/ajax.php\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"analysisFile\"\r\n\r\nC:\\xampp\\htdocs\\website\\phantom\\get_site_info.js\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"businessId\"\r\n\r\n67\r\n------------------------------d33c9f28b99\r\nContent-Disposition: form-data; name=\"website\"\r\n\r\nhttp://www.website.com/\r\n------------------------------ad33c9f28b99--\r\n",
  "url" : "/"
}

如您所见,没有POST对象,只有一个包含所有POST数据的大字符串。这是我通过cURL发送它的方式吗?我对此非常不熟悉,而且我从here获得了cURL代码。

我正在使用casperjs 1.1.0-DEV运行phantomjs 1.9.1,如果有帮助的话。

2 个答案:

答案 0 :(得分:0)

只需调用解析而不是字符串化JSON.parse(yourData);

答案 1 :(得分:0)

除了Sam Aleksovs的回答(使用JSON.parse而非JSON.stringify)。

您需要将数据编码为JSON。你不能只发送数组:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));