问题描述
我在我的PHP应用程序中使用PhantomJS 64位来动态捕获要通过电子邮件发送给用户的HTML页面.
I'm using PhantomJS 64 bit in my PHP application to dynamically capture an HTML page to be emailed to the user.
phantomjs rasterize.js "http://..." /path_to_images/image.png
当我在命令行上运行上述方法时,此方法可以正常工作,但是当PHP脚本使用exec
运行命令时,它将失败,没有输出,并返回退出代码11.
This method works fine when I run the above on the command line but when the PHP script runs the command using exec
it fails with no output and returns exit code 11.
如果我将其切换为使用32位phantomJS二进制文件,该命令将成功执行,但由于错误Reference Error: can't find variable google
,因此无法在页面上加载Google JSAPI.这是一个问题,因为并非所有页面内容都被加载并捕获为图像.使用HTTPS包含JSAPI.如果我切换到HTTP,则参考错误消失了,但是渲染的图像全黑了.
If I switch it to use the 32 bit phantomJS binary, the command succeeds but fails to load the google JSAPI on the page since with error Reference Error: can't find variable google
. This is a problem because not all of the page content is loaded and captured as an image. The JSAPI is included using HTTPS. If I switch to HTTP, the reference error is gone but the rendered image comes out all black.
我以与运行php相同的用户身份测试了该命令.
I tested the command as the same user that php is running as.
总结一下:
command> phantomjs_64 rasterize.js "http://..." /path_to_images/image.png
好
exec('phantomjs_64 rasterize.js "http://..." /path_to_images/image.png');
无输出,退出代码11
No Output, Exit Code 11
command> phantomjs rasterize.js "http://..." /path_to_images/image.png
exec('phantomjs rasterize.js "http://..." /path_to_images/image.png');
输出不完整
有人知道为什么默认的phantomJS rasterize.js脚本在PHP上运行时会失败,或者有解决方法吗?
Does anyone know why the default phantomJS rasterize.js script would fail when running on PHP or have a workaround for this?
推荐答案
更新:此很棒的文章指出了如何在HostGator共享托管上进行这项工作(这是我的问题).只需在命令末尾添加2>&1
即可重定向输出. 64位版本仍然无法使用,但是可以修复32位版本.
UPDATE: This great article by Arlo Carreon points out how to make this work on HostGator shared hosting (this was my problem). Simply add 2>&1
at the end of the command to redirect the output. The 64 bit version still does not work but this fixes the 32 bit version.
事实证明,这仅在通过Apache Web服务器请求PHP脚本时发生.解决方法是为需要接收电子邮件的用户创建数据库条目,并设置cron以执行为数据库中的每个用户条目调用PhantomJS的PHP脚本.如果将cron设置为以最小间隔运行,则用户会认为该电子邮件已生成并立即发送.
It turns out that this only happens when the PHP script is requested through the Apache web server. The workaround is to create a database entry for users that need to receive the email and setup a cron to execute the PHP script that calls PhantomJS for each user entry in the DB. When the cron is set to run at the smallest interval the user perceives that the email was generated and sent immediately.
这篇关于PHP + PhantomJS栅格化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!