How can I send POST data to a phantomjs script

时光怂恿深爱的人放手 提交于 2019-12-02 10:03:16
sjy

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!