Hàm File_Get_Contents() Trong Php


Here is how I would use it in an Ajax điện thoại tư vấn, if I don"t find a pure client way khổng lồ vì this.

Bạn đang xem: Hàm File_Get_Contents() Trong Php

$homepage = file_get_contents("http://www.example.com/");emang đến $homepage;Is there way lớn bởi this client side instead so I don"t have sầu to lớn ajax the string over?


you could vì

JS code:

$.post("phppage.php", url: url , function(data) document.getElementById("somediv").innerHTML = data; );PHPhường code:

$url = $_POST<"url">;eđến file_get_contents($url);That would get you the contents of the url.


JavaScript cannot go out and scrape data off of pages. It can make a Điện thoại tư vấn khổng lồ a local PHP script that then goes on its behalf và grabs the data, but JavaScript (in the browser) cannot vì chưng this.

$.post("/localScript.php", srcToGet: "http://example.com" , function(data) /* From within here, data is whatever your local script sent bachồng to lớn us */);You have options lượt thích JSONPhường & Cross-Origin Resource Sharing at your disposal, but both of those require web1_setting up the other kết thúc, so you cannot just choose a domain name & start firing off requests for data.

Further Reading: Same origin policy


This function will return the tệp tin as a string just like the PHPhường 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 lớn the next statement, not waiting for the data lớn return.

Xem thêm: Không Remember Password Trong Mạng Lan, Control Userpasswords2


It"s 20đôi mươi và some modern approach;

async function file_get_contents(uri, callback) let res = await fetch(uri), ret = await res.text(); return callbaông xã ? callback(ret) : ret; // a Promise() actually.file_get_contents("https://httpbin.org/get", console.log);// orfile_get_contents("https://httpbin.org/get").then(ret => console.log(ret));
Not in a general sense. Cross-domain restrictions disallow Javascript code from doing this.

If the target site has CORS (cross-origin resource sharing) set up, you can use XMLHttpRequest to lớn load files. Most sites vày not, as it"s off by mặc định for security reasons, và is rarely necessary.

If you just need to lớn include an HTML page, you can stichồng it in an element. This is subject to some layout gotchas, though (the page ends up in a fixed-form size element).

Or You can use php.js library. Which allow some php functions for javascript. file_get_contents() function one of them.

You can find more info about php.js : http://phpjs.org/

Thanks for contributing an answer lớn Stack Overflow!

Please be sure to lớn answer the question. Provide details và mô tả your research!

But avoid

Asking for help, clarification, or responding to lớn other answers.Making statements based on opinion; baông xã them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy và cookie policy

Not the answer you're looking for? Browse other questions tagged php javascript or ask your own question.

Is it possible to lớn circumvent the same origin policy if you don't have control of the second origin?
How can I get a html source code of any trang web using jquery/javascript lượt thích php's file_get_content?
Why does my JavaScript code receive sầu a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error, while Postman does not?
site design / hình ảnh © 2021 Staông chồng Exchange Inc; web1_user contributions licensed under cc by-sa. rev2021.5.20.39353

Your privacy

By clicking “Accept all cookies”, you agree Staông xã Exchange can store cookies on your device và discchiến bại information in accordance with our Cookie Policy.