2

我在我的 PHP 应用程序中使用 PhantomJS 64 位来动态捕获要通过电子邮件发送给用户的 HTML 页面。

phantomjs rasterize.js "http://..." /path_to_images/image.png

当我在命令行上运行上述命令时,此方法工作正常,但是当 PHP 脚本使用exec它运行命令时,它会失败且没有输出并返回退出代码 11。

如果我将其切换为使用 32 位 phantomJS 二进制文件,该命令会成功,但无法在页面上加载 google JSAPI,因为出现错误Reference Error: can't find variable google。这是一个问题,因为并非所有页面内容都被加载并捕获为图像。JSAPI 使用 HTTPS 包含在内。如果我切换到 HTTP,参考错误就消失了,但渲染的图像全黑了。

我以与运行 php 的用户相同的用户身份测试了该命令。

把它们加起来:

command> phantomjs_64 rasterize.js "http://..." /path_to_images/image.png

好的

exec('phantomjs_64 rasterize.js "http://..." /path_to_images/image.png');

无输出,退出代码 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 上运行时会失败或有解决方法?

4

1 回答 1

0

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.

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.

于 2014-01-28T21:33:01.973 回答