简体   繁体   English

如何将POST数据发送到phantomjs脚本

[英]How can I send POST data to a phantomjs script

I am working with PHP/CURL and would like to send POST data to my phantomjs script, by setting the postfields array below: 我正在使用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 (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. 问题是我不知道如何在phantomjs脚本中解析POST请求。 I am using the webserver module to expose the script. 我正在使用Web服务器模块公开脚本。

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: 我怀疑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();
    })

Can someone show me how to parse a POST request here? 有人可以告诉我如何在此处解析POST请求吗?

The request.post object contains the body of the POST request. request.post对象包含POST请求的主体。 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 . 如果$postFieldArray确实是一个数组,则PHP(至少根据此答案 )应该已经对该数组进行了编码,并使用内容类型x-www-form-urlencoded Actually, according to the PHP documentation : 实际上,根据PHP文档

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. 将数组传递给CURLOPT_POSTFIELDS会将数据编码为multipart / form-data,而传递URL编码的字符串会将数据编码为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. 尽管在API参考中没有明确指出 ,但此GitHub问题建议PhantomJS将x-www-form-urlencoded表单的内容作为属性公开给request.post对象。 That's what seems to be happening in the example ( request.post.url refers to the form field url ). 这就是示例中正在发生的事情( request.post.url引用表单字段url )。 The easiest way to check would be to log the request.post object to the console and see what's in there. 最简单的检查方法是将request.post对象记录到控制台,然后查看其中的内容。

However, the GitHub issue also implies that multipart/form-data is not supported by the PhantomJS webserver. 但是,GitHub问题还意味着PhantomJS Web服务器不支持multipart/form-data So, unless you're prepared to change to a different web server, it might be easiest just to encode the data with JSON. 因此,除非您准备更改为其他Web服务器,否则仅使用JSON编码数据可能是最简单的。 On the PHP side: 在PHP方面:

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

And then on the PhantomJS side: 然后在PhantomJS方面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM