简体   繁体   English

使用PHP在服务器端获取Javascript /浏览器呈现的元素

[英]get Javascript / Browser rendered elements on serverside with PHP

This is a pretty broad question, but I'm hoping there is a solution. 这是一个相当广泛的问题,但我希望有一个解决方案。 I realize that trying to do anything serverside that is done client side is kind of a contradiction in terms, but am hoping there is a way to emulate client side page rendering in PHP. 我意识到尝试做任何在客户端完成的服务器端工作有点矛盾,但是我希望有一种方法可以模拟PHP中的客户端页面渲染。

I have a website that converts reports generated with php/mysql as well as data from an api. 我有一个网站,可以转换用php / mysql生成的报告以及来自api的数据。 I am also using chart.js to display graphs. 我也在使用chart.js来显示图表。 My client wants to be able to send these reports on a monthly basis via email to his clients. 我的客户希望能够每月通过电子邮件将这些报告发送给他的客户。 I figure that cron jobs would accomplish this, but am now stuck with how to generate the content without a browser. 我认为cron作业可以完成此任务,但现在仍然受困于如何在没有浏览器的情况下生成内容。

The site is hosted on godaddy, so I don't have comand line access or anything like that. 该网站托管在godaddy上,因此我没有comand线访问权限或类似功能。 Basically just ftp and mysql access. 基本上只是ftp和mysql访问。

If you have ssh access, then this job becomes much easier by phantomjs and your api. 如果您具有ssh访问权限,那么phantomjs和您的api会使这项工作变得容易得多。 Create HTML report by phantomjs and get the generated contents by file-get-content. 通过phantomjs创建HTML报告,并通过file-get-content获取生成的内容。 Then send it by email. 然后通过电子邮件发送。

A few options here I think. 我认为这里有一些选择。 In the specific case you've outlined, only 1 and 2 are appropriate, but I've popped 3 in as it might help others. 在您概述的特定情况下,仅1和2是合适的,但是我弹出3是因为它可能会帮助其他人。 Side note, if GoDaddy has cPannel, you should be able to set up cron jobs from there, and then Option 2 might be most appropriate. 旁注,如果GoDaddy具有cPannel,则应该能够从那里设置cron作业,然后Option 2可能是最合适的。

Option 1 (least work): 选项1 (最少的工作):

After rendering POST the image back to the server for emailing. 渲染后,将图像发送回服务器以通过电子邮件发送。

Option 2 (least technology): 选项2 (最低技术):

Use a PHP chart library for both server side and client side charts. 将PHP图表库用于服务器端和客户端图。 Remove the old code. 删除旧代码。

Option 3 (seperation of concerns): 选项3 (关注点分离):

Run a seperate NodeJS service so that you can do server side rendering using the same code you'd use for client side rendering. 运行单独的NodeJS服务,以便可以使用与客户端渲染相同的代码进行服务器端渲染。 Call to it from the PHP service to get the images rendered. 从PHP服务对其进行调用以获取呈现的图像。

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

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