在Javascript使用PHP运行后获取URL的内容(文本) [英] Get the content (text) of an URL after Javascript has run with PHP

查看:167
本文介绍了在Javascript使用PHP运行后获取URL的内容(文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用PHP获取URL的内容(使用某种函数,如file_get_contents或header),但只能在执行一些Javascript代码之后?



示例:



mysite.com有一个脚本 loadUrlAfterJavascriptExec('http://exampletogetcontent.com/')并打印/回显内容。想象一下,一些jQuery在改变DOM的 http://exampletogetcontent.com/ 上运行,而 loadUrlAfterJavascriptExec HTML



我们能做到吗?



@Edit



不知道我是否清楚了,但是我想要的是通过URL获取页面的内容,但是只有在目标页面上运行Javascript之后(一个PHP获取其内容)



我知道PHP在页面发送到客户端之前运行,JS之后,但是mabe有一个专家解决方法

解决方案

更新2 添加更多关于如何使用PHP中的 phantomjs

更新1 (在说明目标页面上的JavaScript需要先运行后)



方法1:使用 phantomjs (将执行javascript);



1。下载 phantomjs ,并将可执行文件放在PHP二进制文件可以访问的路径中。



2。将以下2个文件放在同一目录中:



get-website.php

 <?php 

$ phantom_script = dirname(__ FILE__)。 '/get-website.js';


$ response = exec('phantomjs'。$ phantom_script);

echo htmlspecialchars($ response);
?>

get-website.js

  var webPage = require('webpage'); 
var page = webPage.create();

page.open('http://google.com/',function(status){
console.log(page.content);
phantom.exit() ;
});

3。浏览至 get-website。 php ,目标网站, http://google.com 内容将在执行内联JavaScript后返回。您也可以使用 php /path/to/get-website.php 从命令行调用此方法。



方法2:使用Ajax与PHP(无phantomjs,因此不会运行javascript);



/get-website.php

 <?php 

$ html = file_get_contents('http://google.com');
echo $ html;
?>

test.html

 <!doctype html> 
< html lang =en>
< head>
< meta charset =utf-8>
< title> on demo< / title>
< style>
p {
color:red;
}
span {
color:blue;
}
< / style>
< script src =https://code.jquery.com/jquery-1.10.2.js>< / script>
< / head>
< body>
< button id ='click_me'>点击我< / button>
< span style =display:none;>< / span>
< script>

$(#click_me).click(function(){
$ .get(/ get-website.php,function(data){
var json = {
html:JSON.stringify(data),
delay:1
};
alert(json.html);
});
});
< / script>
< / body>
< / html>


Is it possible to get the content of an URL with PHP (using some sort of function like file_get_contents or header) but only after the execution of some Javascript code?

Example:

mysite.com has a script that does loadUrlAfterJavascriptExec('http://exampletogetcontent.com/') and prints/echoes the content. imagine that some jQuery runs on http://exampletogetcontent.com/ that changes DOM, and loadUrlAfterJavascriptExec will get the resulting HTML

Can we do that?

@Edit

Not sure if I made myself clear, but what I wanted was to get the content of a page, through an URL, but only after Javascript run on the target page (the one PHP is getting its content)

I am aware PHP runs before the page is sent to the client, and JS only after that, but mabe there was an expert workaround

解决方案

Update 2 Adds more details on how to use phantomjs from PHP.

Update 1 (after clarification that javascript on target page need to run first)

Method 1:Use phantomjs(will execute javascript);

1. Download phantomjs and place the executable in a path that your PHP binary can reach.

2. Place the following 2 files in the same directory:

get-website.php

<?php

    $phantom_script= dirname(__FILE__). '/get-website.js'; 


    $response =  exec ('phantomjs ' . $phantom_script);

    echo  htmlspecialchars($response);
    ?>

get-website.js

var webPage = require('webpage');
var page = webPage.create();

page.open('http://google.com/', function(status) {
 console.log(page.content);
  phantom.exit();
});

3. Browse to get-website.php and the target site, http://google.com contents will return after executing inline javascript. You can also call this from a command line using php /path/to/get-website.php.

Method 2:Use Ajax with PHP (No phantomjs so won't run javascript);

/get-website.php

<?php

    $html=file_get_contents('http://google.com');
    echo $html;
    ?>

test.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>on demo</title>
<style>
p {
color: red;
}
span {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<button id='click_me'>Click me</button>
<span style="display:none;"></span>
<script>

$( "#click_me" ).click(function () {
    $.get("/get-website.php", function(data) {
        var json = {
            html: JSON.stringify(data),
            delay: 1
        };
        alert(json.html);
        });
});
</script>
</body>
</html>

这篇关于在Javascript使用PHP运行后获取URL的内容(文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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