PHP获取呈现的Javascript页面 [英] PHP Get Rendered Javascript Page

查看:65
本文介绍了PHP获取呈现的Javascript页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AngularJS开发应用程序.一切似乎都很好,直到我遇到令我头痛的东西:SEO.

I'm developing application using AngularJS. Everything seems to be nice until I meet something that leads me to headache: SEO.

从许多参考资料中,我发现Google bot或Bing bot抓取并建立索引的AJAX内容并不那么容易",因为这些抓取工具无法呈现Javascript.

From many references, I found out that AJAX content crawled and indexed by Google bot or Bing bot 'is not that easy' since the crawlers don't render Javascript.

当前,我需要使用PHP的解决方案.我使用PHP Slim Framework,所以我的主文件是index.php,其中包含用于回显index.html内容的函数.我的问题是:

Currently I need a solution using PHP. I use PHP Slim Framework so my main file is index.php which contains function to echo the content of my index.html. My question is:

是否可以在HTML中制作呈现的Javascript快照?

我的策略是:

如果请求查询字符串包含_escaped_fragment_,则应用程序将生成一个快照并将该快照作为响应,而不是确切的文件.

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thanks.

推荐答案

经过大量的搜索和研究,我终于设法通过将PHP与PhantomJS(2.0版)混合使用来解决我的问题.我在PHP中使用exec()函数运行phantomJS并创建Javascript文件以获取目标URL的内容.以下是代码段:

After plenty of times searching and researching, I finally managed to solve my problem by mixing PHP with PhantomJS (version 2.0). I use exec() function in PHP to run phantomJS and create Javascript file to take get the content of the targeted URL. Here are the snippets:

index.php

// Let's assume that you have a bin folder under your root folder directory which contains phantomjs.exe and content.js
$script = __DIR__ ."/bin/content.js";
$target = "http://www.kincir.com"; // target URL
$cmd = __DIR__."/bin/phantomjs.exe $script $target";
exec($cmd, $output);
return implode("", $output);

content.js

var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
var url = system.args[1]; // This will get the second argument from $cmd, in this example, it will be the value of $target on index.php which is "http://www.kincir.com" 
page.open(url, function (status) {
  page.onLoadFinished = function () { // Make sure to return the content of the page once the page is finish loaded
      var content = page.content;
      console.log(content);
      phantom.exit();
  };
});

这篇关于PHP获取呈现的Javascript页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆