是否有 JavaScript 方法来执行 file_get_contents()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10693518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a JavaScript way to do file_get_contents()?
提问by CS_2013
Here is the PHP documentation
这是PHP 文档
Here is how I would use it in an Ajax call, if I don't find a pure client way to do this.
如果我找不到纯粹的客户端方式来执行此操作,我将在 Ajax 调用中使用它的方法如下。
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
Is there way to do this client side instead so I don't have to ajax the string over?
有没有办法做这个客户端,所以我不必将字符串放在 ajax 上?
采纳答案by PitaJ
you could do
你可以
JS code:
JS代码:
$.post('phppage.php', { url: url }, function(data) {
document.getElementById('somediv').innerHTML = data;
});
PHP code:
PHP代码:
$url = $_POST['url'];
echo file_get_contents($url);
That would get you the contents of the url.
这会让你得到 url 的内容。
回答by Sampson
JavaScript cannot go out and scrape data off of pages. It can make a call to a local PHP script that then goes on its behalf and grabs the data, but JavaScript (in the browser) cannot do this.
JavaScript 无法从页面上抓取数据。它可以调用本地 PHP 脚本,然后代表它获取数据,但是 JavaScript(在浏览器中)不能这样做。
$.post("/localScript.php", { srcToGet: 'http://example.com' }, function(data){
/* From within here, data is whatever your local script sent back to us */
});
You have options like JSONP and Cross-Origin Resource Sharing at your disposal, but both of those require setting up the other end, so you cannot just choose a domain and start firing off requests for data.
您可以使用 JSONP 和跨域资源共享等选项,但这两者都需要设置另一端,因此您不能只选择一个域并开始触发数据请求。
Further Reading: Same origin policy
延伸阅读:同源政策
回答by Steve Lage
This function will return the file as a string just like the PHP file_get_contents()
.
这个函数将文件作为字符串返回,就像 PHP 一样file_get_contents()
。
function file_get_contents(uri, callback) {
fetch(uri).then(res => res.text()).then(text => callback(text));
}
However unlike PHP, JavaScript will go on to the next statement, not waiting for the data to return.
但是与 PHP 不同的是,JavaScript 会继续执行下一条语句,而不是等待数据返回。
回答by duskwuff -inactive-
Not in a general sense. Cross-domain restrictions disallow Javascript code from doing this.
不是一般意义上的。跨域限制不允许 Javascript 代码执行此操作。
If the target site has CORS(cross-origin resource sharing) set up, you can use XMLHttpRequest to load files. Most sites do not, as it's off by default for security reasons, and is rarely necessary.
如果目标站点设置了CORS(跨域资源共享),则可以使用 XMLHttpRequest 加载文件。大多数网站都没有,因为出于安全原因,默认情况下它是关闭的,而且很少有必要。
If you just need to include an HTML page, you can stick it in an <iframe>
element. This is subject to some layout gotchas, though (the page ends up in a fixed-size element).
如果您只需要包含一个 HTML 页面,您可以将其粘贴在一个<iframe>
元素中。不过,这会受到一些布局问题的影响(页面以固定大小的元素结束)。
回答by Fazle Elahee
Or You can use php.js library. Which allow some php functions for javascript. file_get_contents() function one of them.
或者您可以使用 php.js 库。这允许一些用于 javascript 的 php 函数。file_get_contents() 函数是其中之一。
<script>
var data = file_get_contents('Your URL');
</script>
You can find more info about php.js : http://phpjs.org/
您可以找到有关 php.js 的更多信息:http://phpjs.org/
回答by K-Gun
It's 2020 and some modern approach;
现在是 2020 年和一些现代方法;
async function file_get_contents(uri, callback) {
let res = await fetch(uri),
ret = await res.text();
return callback ? callback(ret) : ret; // a Promise() actually.
}
file_get_contents("https://httpbin.org/get", console.log);
// or
file_get_contents("https://httpbin.org/get").then(ret => console.log(ret));