Is this possible? If so, how could I make it where you give it a URL, and it will return the page data (as in, the whole data, the <html>, everything). Specifically, I want to use this with javascript to edit the html and use the edited source in an iframe.
- KermMartian
- Site Admin (Posts: 64066)
- Re: Get the data from a page using PHP
- 12 Sep 2010 02:17:27 pm
_player1537 wrote:
Is this possible? If so, how could I make it where you give it a URL, and it will return the page data (as in, the whole data, the <html>, everything). Specifically, I want to use this with javascript to edit the html and use the edited source in an iframe.
The simplest way to do this is with the file_get_contents() function, which works on URLs as well as local files:
http://us.php.net/file_get_contents
However, it often does not work under restricted webhosting, in which case you'll need to use the cURL library instead.
What would be the way to give the data this gets to javascript? Or would it be best to just edit the source using PHP?
_player1537 wrote:
What would be the way to give the data this gets to javascript? Or would it be best to just edit the source using PHP?
Well, you can directly read the page clientside with Javascript by using the XMLHttpRequest object (the core of most AJAX).
I tried to do the xmlHttprequest thing, but (and this is for jBOS) it didn't work >.<
I got this error:
Code:
from this code:
Code:
I got this error:
Code:
uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///C:/Users/Tanner/Desktop/JBOS/apps/Test/Test.js :: Test_open :: line 5" data: no]
from this code:
Code:
function Test_open()
{
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false);
req.send(null);
if(req.status == 200)
window_create("tawr",100,100,100,100,req.responseText);
}
So, it's *asynchronous, so you need to do something more like this:
Code:
Disclaimer: code more or less stolen from:
http://www.w3.org/TR/XMLHttpRequest/
Code:
function function handler() {
if(this.readyState == 4 && this.status == 200) {
// so far so good
if(this.responseXML != null)
// success!
alert(this.responseXML);
else
test(null);
} else if (this.readyState == 4 && this.status != 200) {
// fetched the wrong page or network error...
alert('OHNOFAIL');
}
}
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml");
client.send();
Disclaimer: code more or less stolen from:
http://www.w3.org/TR/XMLHttpRequest/
You also can't do an XMLHttpRequest to another domain, it *must* be the same domain that the JavaScript is running from. So you'll need to bounce if off of the PHP server that is hosting the JS.
Kllrnohj wrote:
You also can't do an XMLHttpRequest to another domain, it *must* be the same domain that the JavaScript is running from. So you'll need to bounce if off of the PHP server that is hosting the JS.
Indeed, except that I think his object is local files from the local filesystem, which is acceptable. If it's something from another domain, you'll need to use a PHP grabber. The purpose of this is to avoid XSS eploits. KermMartian wrote:
Indeed, except that I think his object is local files from the local filesystem, which is acceptable. If it's something from another domain, you'll need to use a PHP grabber. The purpose of this is to avoid XSS eploits.
Code:
req.open('GET', 'http://www.mozilla.org/', false);
mozilla.org is located on his filesystem? News to me
Well yeah, that part was just a test. I suppose that's another reason why his example didn't work, but I was thinking of his eventual goal.
- DJ Omnimaga
- Guru-in-Training (Posts: 2836)
- Re: Get the data from a page using PHP
- 12 Sep 2010 05:13:59 pm
KermMartian wrote:
_player1537 wrote:
Is this possible? If so, how could I make it where you give it a URL, and it will return the page data (as in, the whole data, the <html>, everything). Specifically, I want to use this with javascript to edit the html and use the edited source in an iframe.
The simplest way to do this is with the file_get_contents() function, which works on URLs as well as local files:
http://us.php.net/file_get_contents
However, it often does not work under restricted webhosting, in which case you'll need to use the cURL library instead.
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Advertisement