Using PHP To Get An External File
02-19-2008, 10:02 AM
|
Using PHP To Get An External File
|
Posts: 5
|
Hi guys, I'm looking for a way in PHP to open an external file, as in on another server. So for example if I wanted to open the first page of http://www.webmaster-talk.com it would collect the file and let me display the HTML of the file. Obviously REQUIRE and INCLUDE won't work and I tried FPOPEN but didn't have much success. Any ideas?
|
|
|
|
02-19-2008, 10:56 AM
|
Re: Using PHP To Get An External File
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
You have to use the ftp extensions of PHP for that.
http://www.php.net/manual/en/ref.ftp.php
Obviously, it means that you need to know the ftp login to reaches the pages of the site you want to open.
If you don't, it will work on a certain extend on static sites, as you will be able to get referenced ressources (pages, pdf, images...), but you won't be able to see the files that are not linked from the content of the html file you take as a reference.
And on dynamic sites using asp or php, there is no way from the outside to get the sources. The language embedded in them is integrated on the server side, often fetched with datas coming from a database, and to replicate the site, you will need the asp or php sources (which you cannot reach from a web browser, except when especially declared so, at least in php. I believe that you cannot at all on asp.net) , the database schema, and the database datas.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
02-19-2008, 11:00 AM
|
Re: Using PHP To Get An External File
|
Posts: 5
|
Thank you very much. All I'm looking to do is take the HTML of the main page, I don't need FTP logins or any sort of dynamic scripting. Thanks again.
|
|
|
|
02-19-2008, 11:09 AM
|
Re: Using PHP To Get An External File
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
If you just want the html of a page, then a simple file_get_contents()
http://www.php.net/manual/en/functio...t-contents.php
is enough, if your server PHP configuration allows the use of this function.
Otherwise, a simple CURL function can do the same:
PHP Code:
/** * Fetch the specified page source, even if fopen_url_wrapper is disabled * For that purpose, we use the curl wrapper. It should work everywhere * * @param String $url The url to fetch * @return String The page source code */ function getPage($url="http://www.example.com/"){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/'); curl_setopt($ch,CURLOPT_TIMEOUT,10); $html=curl_exec($ch); if($html==false){ $m=curl_error(($ch)); error_log($m); } curl_close($ch); return $html; }
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
02-19-2008, 11:51 AM
|
Re: Using PHP To Get An External File
|
Posts: 5
|
Thanks again, but I'm unsure how to implement these properly. When I run the curl script runs I just get a blank page and I can't get any of the to work file_get_contents functions to work. Could you please provide an example for my stupid self? Thank you so much.
|
|
|
|
02-19-2008, 01:19 PM
|
Re: Using PHP To Get An External File
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
That code was just a function, you need to call it tho use it.
[php]
PHP Code:
/** * Fetch the specified page source, even if fopen_url_wrapper is disabled * For that purpose, we use the curl wrapper. It should work everywhere * * @param String $url The url to fetch * @return String The page source code */ function getPage($url="http://www.example.com/"){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/'); curl_setopt($ch,CURLOPT_TIMEOUT,10); $html=curl_exec($ch); if($html==false){ $m=curl_error(($ch)); error_log($m); } curl_close($ch); return $html; }
$html=getPage("http://www.webmaster-talk.com/"); $html=htmlentities($html); echo $html;
This will echo the source code of the index page of WMT.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
02-19-2008, 08:20 PM
|
Re: Using PHP To Get An External File
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
It sounds like your curl wrappers are disabled for your hosting account.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
02-20-2008, 04:50 AM
|
Re: Using PHP To Get An External File
|
Posts: 5
|
Quote:
Originally Posted by tripy
That code was just a function, you need to call it tho use it.
[php]
PHP Code:
/** * Fetch the specified page source, even if fopen_url_wrapper is disabled * For that purpose, we use the curl wrapper. It should work everywhere * * @param String $url The url to fetch * @return String The page source code */ function getPage($url="http://www.example.com/"){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/'); curl_setopt($ch,CURLOPT_TIMEOUT,10); $html=curl_exec($ch); if($html==false){ $m=curl_error(($ch)); error_log($m); } curl_close($ch); return $html; }
$html=getPage("http://www.webmaster-talk.com/"); $html=htmlentities($html); echo $html;
This will echo the source code of the index page of WMT.
|
That works perfectly, thank you so much.
|
|
|
|
|
« Reply to Using PHP To Get An External File
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|