Michdd Posted October 15, 2008 Share Posted October 15, 2008 Is there a way that I can get the source of a .php file. Not what's in the file, but what the file outputs. I tried using file_get_contents() but that just gets the server-side code. I want to be able to get what the code outputs, Like exactly what you'd see if you viewed the page's source. Then store it in a variable, I can't find a solution to this problem. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/ Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 if you use file_get_contents() on the URL of the file (not local path) you will get the output of the file... or if you want it in the current scope you can include() it into a buffer and clean/close the buffer. It's likely that you are approaching the problem incorrectly though, why would you need to do this? Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665716 Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 i agree, opening it via a HTTP protocal will parsa the file thus giving you what you want.. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665718 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 i agree, opening it via a HTTP protocal will parsa the file thus giving you what you want.. I want the exact source. Â But it's on an external url. And I can't get it to work... What would I have to use? Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665719 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 What do you mean you want the exact source?  If you want google's html you would just do  $googleshtml = file_get_contents('http://www.google.com/'); Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665720 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 Yea, but I'm trying to get a .php's file source, and when I do that it just tries to run the script. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665721 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 Oh, if you want the source of the file, it has to be on your server, it's impossible to get someone else's php code because it's parsed by their server before it's sent to you.... Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665722 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 Oh, if you want the source of the file, it has to be on your server, it's impossible to get someone else's php code because it's parsed by their server before it's sent to you....  nono... I don't want the source. Like if someone had a file:  <?php echo "Hello.<br />hello"; ?>  When you right click > View page source.  You'd see.  Hello. hello  Or whatever. That's what I want. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665724 Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 Oh, if you want the source of the file, it has to be on your server, it's impossible to get someone else's php code because it's parsed by their server before it's sent to you.... Agreed your need FTP access or a add handler update if its your code then use FTP if its not then write your own  EDIT: for the HTML source use $googleshtml = file_get_contents('http://www.google.com/');  ie my file dummy.php is on the root of mydomain.com so instead of $html = file_get_contents('dummy.php'); you would do $html = file_get_contents('http://mydomain.com/dummy.php');  make sense ? Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665725 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 Okay, let's go slowly. Is this file on your server or on someone else's website? If it's on yours, try doing this.. Â <?php function getExecContent($file) { Â ob_start(); Â require($file); Â return ob_get_clean(); } $contents = getExecContent('/the/path/to/my/file.php'); Â If it's on someone else's (like google's), try doing what I said before... Â $contents = file_get_contents('http://www.google.com/'); Â but this will still retrieve "Hello.<br />hello" not "Hello. hello" Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665728 Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 i think he means its on his server BUT wants the parser'd version see my last post Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665729 Share on other sites More sharing options...
AndyB Posted October 15, 2008 Share Posted October 15, 2008 If there was a prize for the largest number of confused people in a thread, this one would be a front-runner. Â You start by asking how you get the source code - which can't be done unless you have server access to the domain where the script resides. Then you switch to wanting to see the generated html which can be done by 'view source' using a browser. Â Could you clarify just what you want to get and how you're going to use it if you could get it. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665732 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 Mine or madtechie's code will work fine.. mine is more portable, and his is more readable. Take your pick. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665737 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 Okay, let's go slowly. Is this file on your server or on someone else's website? If it's on yours, try doing this.. Â <?php function getExecContent($file) { Â ob_start(); Â file_get_contents($file); Â return ob_get_clean(); } $contents = getExecContent('/the/path/to/my/file.php'); Â If it's on someone else's (like google's), try doing what I said before... Â $contents = file_get_contents('http://www.google.com/'); Â It's on someone else's server. When I just try what you said I get this: Â failed to open stream: Connection timed out in /home/a1280292/public_html/onlinetfg.php on line 3 Â However, the page works. And if I try to get it in like a html frame it works. Someone said that it's not working because it's trying to get the actual server-side code. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665738 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 can you post your full code here along with the url you are attempting to get content from? Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665740 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 I was just testing it with that one function for now. What I plan to do is get the source, store it in a variable, then run strpos() so I can search for something. Because the source will be updating from the other server. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665743 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 Can you post the url here or not so we can test it ourselves? Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665745 Share on other sites More sharing options...
Michdd Posted October 15, 2008 Author Share Posted October 15, 2008 Can you post the url here or not so we can test it ourselves? <?php $content = file_get_contents('http://tfg.game-host.org:8080/onlinelist.php'); echo $content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665747 Share on other sites More sharing options...
genericnumber1 Posted October 15, 2008 Share Posted October 15, 2008 The reason you're getting a timeout error is very likely because the website is loading slowly, that's all. Your code works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665750 Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 try putting set_time_limit(0); at the start Quote Link to comment https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/#findComment-665752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.