Using php to collect the source code of a page

Discussion in 'PHP' started by Crayz, Feb 5, 2007.

  1. #1
    Hello, is it possible to collect a source code from a specified page using php?

    Thanks
     
    Crayz, Feb 5, 2007 IP
  2. duilen

    duilen Active Member

    Messages:
    354
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #2
    duilen, Feb 5, 2007 IP
  3. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #3
    This seems to collect the actual code of the page, unless I messed up somewhere.

    Is it possible to collect the html source that would appear if you went to View -> Source ?

    Thanks :)
     
    Crayz, Feb 5, 2007 IP
  4. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Use curl it might help you. visit php.net , you will get good help there about curl. I will soon send some example to capture source using curl.
     
    technoguy, Feb 5, 2007 IP
  5. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #5
    Ok, i'll see what I can do with it, thanks :)
     
    Crayz, Feb 5, 2007 IP
  6. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    
    <? 
    $handle = fopen("http://www.example.com/", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer .= fgets($handle, 4096);
        }
        fclose($handle);
        echo $buffer;  //you will get all source in this buffer
    }
    
     ?>
    
    Code (markup):
    let me know if it works for you
     
    rays, Feb 5, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    file_get_contents, file, fread, and curl will all do the same thing, they will retrieve the content as parsed by the server that the file you're requesting resides on, and it is infact exactly the same source that Internet Explorer, Mozilla, Firefox, and any other browser or http1.0/1.1 || > client will retrieve.

    Hope that clears it up.

    If you're looking to get the source code of a php script, you'll have no luck at all, there's no way to achieve such things, php is a server side language; all php code is parsed before the content gets to a user / script / client / bot / crawler or whatever else .....
     
    krakjoe, Feb 6, 2007 IP
  8. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Maybe... just maybe a program called flashget will work. It's freeware. Too bad I can't try it out now (not at home)...
     
    Choller, Feb 7, 2007 IP
  9. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #9
    Oh I see.

    Would it be possible to turn a portion of php code into html the way it would be as viewed in a browser, and store the html code into a variable?

    Thanks.
     
    Crayz, Feb 7, 2007 IP
  10. livingearth

    livingearth Well-Known Member

    Messages:
    1,469
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    140
    #10
    The above stated methods do just that, however if you view that code in a browser it will appear the same as viewing the page itself. Maybe you could pass the html code to a text field where it would be viewed as code...
     
    livingearth, Feb 7, 2007 IP
  11. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #11
    Well what I am looking to do is get my php code to turn into the html it would display when you view it in a browser, and put that html data into a variable.

    It doesn't necessarily have to be through getting the source from a page. Any way possible will do.
     
    Crayz, Feb 7, 2007 IP
  12. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #12
    
    $var = file_get_contents("http://location.com");
    
    PHP:
     
    krakjoe, Feb 8, 2007 IP
  13. dptips

    dptips Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi,
    I am not sure whether this will help u.Try this link
    http://au2.php.net/fopen

    In that page, there is a class named HttpRequest.U can use that class to get the page source of an url.
     
    dptips, Feb 8, 2007 IP
  14. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #14
    no there isn't, classes aren't reference by .'s, just a friendly suggestion if you don't know the answer to a question then don't try to answer it you'll just create more confusion for the OP
     
    krakjoe, Feb 8, 2007 IP
  15. rossdalangin

    rossdalangin Well-Known Member

    Messages:
    458
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #15
    you can get it if you are in the same server. It's like copying a file. As crackjoe said "file_get_contents, file, fread, and curl will all do the same thing". If there's such thing then no one will buy a script.
     
    rossdalangin, Feb 10, 2007 IP
  16. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #16
    cURL is the quickest way to retrieve the information, more advanced but also more powerful than the other options.
     
    smatts9, Feb 10, 2007 IP
  17. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #17
    <?php
    	function getPage ($url) {
    		if (function_exists('curl_init')) {
    			$ch = curl_init($url);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    			return curl_exec($ch);
    		} else {
    			return file_get_contents($url);
    		}
    	}
    ?>
    PHP:
    Neat little function. If you don't have cURL, it goes for file_get_contents. Otherwise, cURL away :D
     
    -NB-, Feb 10, 2007 IP
  18. rossdalangin

    rossdalangin Well-Known Member

    Messages:
    458
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #18
    curl_init
    (PHP 4 >= 4.0.2, PHP 5)

    curl_init -- Initialize a cURL session
    Description
    resource curl_init ( [string url] )


    Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.


    It was just the same. ;)
     
    rossdalangin, Feb 17, 2007 IP
  19. lmoss

    lmoss Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    You could try using output buffering. Assuming the PHP code you want to evaluate is in a variable named $phpCode:

    <?php
    ob_start();
    eval($phpCode);
    $result = ob_get_contents();
    ob_end_clean();
    ?>
    Code (markup):
    See here for more details on output buffering: http://www.php.net/manual/en/ref.outcontrol.php
     
    lmoss, Feb 19, 2007 IP