Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
Using PHP To Get An External File
Old 02-19-2008, 10:02 AM Using PHP To Get An External File
Novice Talker

Posts: 5
Trades: 0
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?
Pope Pius X2 is offline
View Public Profile
 
 
Register now for full access!
Old 02-19-2008, 10:56 AM Re: Using PHP To Get An External File
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-19-2008, 11:00 AM Re: Using PHP To Get An External File
Novice Talker

Posts: 5
Trades: 0
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.
Pope Pius X2 is offline
View Public Profile
 
Old 02-19-2008, 11:09 AM Re: Using PHP To Get An External File
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-19-2008, 11:51 AM Re: Using PHP To Get An External File
Novice Talker

Posts: 5
Trades: 0
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.
Pope Pius X2 is offline
View Public Profile
 
Old 02-19-2008, 01:19 PM Re: Using PHP To Get An External File
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-19-2008, 08:20 PM Re: Using PHP To Get An External File
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
It sounds like your curl wrappers are disabled for your hosting account.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
View Public Profile
 
Old 02-20-2008, 04:50 AM Re: Using PHP To Get An External File
Novice Talker

Posts: 5
Trades: 0
Quote:
Originally Posted by tripy View Post
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.
Pope Pius X2 is offline
View Public Profile
 
Closed Thread     « Reply to Using PHP To Get An External File
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26387 seconds with 12 queries