如何将POST数据发送到phantomjs脚本 [英] How can I send POST data to a phantomjs script

查看:437
本文介绍了如何将POST数据发送到phantomjs脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP / CURL,并希望通过设置下面的postfields数组将POST数据发送到我的phantomjs脚本:

  $ 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(兼容; 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脚本中的POST请求。我正在使用webserver模块来公开脚本。



我怀疑 https://github.com/benfoxall/phantomjs-webserver-example/blob/master/server.js 可能有答案,但我不知道知道足够的javascript来判断是否正在解析后期变量:

  var service = server.listen(port,function(request,response) ){

if(request.method =='POST'&& request.post.url){
var url = request.post.url;

request_page(url,function(properties,imageuri){
response.statusCode = 200;
response.write(JSON.stringify(properties));
response.write(\ n);
response.write(imageuri);
response.close();
})

有人可以告诉我如何在这里解析POST请求吗?

解决方案

request.post 对象包含POST请求的主体。如果 $ postFieldArray 确实是一个数组,那么(至少根据这个答案) )PHP应编码数组并使用内容类型 x-www-form-urlencoded 将其发布到。实际上,根据 PHP文档


将数组传递给CURLOPT_POSTFIELDS会将数据编码为multipart / form-data,而传递URL编码的字符串会将数据编码为application / x-www- form-urlencoded。


虽然 API参考,这个 GitHub问题建议PhantomJS将 x-www-form-urlencoded 表格的内容公开为 request.post 对象。这就是示例中似乎发生的事情( request.post.url 是指表单字段 url )。最简单的检查方法是将 request.post 对象记录到控制台,看看里面有什么。



但是,GitHub问题还暗示PhantomJS网络服务器不支持 multipart / form-data 。因此,除非您准备更改为其他Web服务器,否则使用JSON对数据进行编码可能最简单。在PHP方面:

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

然后在PhantomJS方面:

  var data = JSON.parse(request.post); 


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

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

The problem is I don't know how to parse a POST request inside the phantomjs script. I am using the webserver module to expose the script.

I suspect https://github.com/benfoxall/phantomjs-webserver-example/blob/master/server.js may have the answer, but I don't know enough javascript to tell whether post variables are being parsed:

var service = server.listen(port, function(request, response) {

if(request.method == 'POST' && request.post.url){
    var url = request.post.url;

    request_page(url, function(properties, imageuri){
        response.statusCode = 200;
        response.write(JSON.stringify(properties)); 
        response.write("\n");   
        response.write(imageuri);
        response.close();
    })

Can someone show me how to parse a POST request here?

解决方案

The request.post object contains the body of the POST request. If your $postFieldArray is indeed an array, then (at least according to this answer) PHP should have encoded the array and POSTed it with the content type x-www-form-urlencoded. Actually, according to the PHP documentation:

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

Although it is not explicit in the API reference, this GitHub issue suggests that PhantomJS will expose the content of an x-www-form-urlencoded form as properties on the request.post object. That's what seems to be happening in the example (request.post.url refers to the form field url). The easiest way to check would be to log the request.post object to the console and see what's in there.

However, the GitHub issue also implies that multipart/form-data is not supported by the PhantomJS webserver. So, unless you're prepared to change to a different web server, it might be easiest just to encode the data with JSON. On the PHP side:

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

And then on the PhantomJS side:

var data = JSON.parse(request.post);

这篇关于如何将POST数据发送到phantomjs脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆