Get HTML source in PHP

Discussion in 'PHP' started by Helado, Jul 24, 2010.

  1. #1
    Hi all,

    I want to get the source code of a page in PHP. There is a page, lets say google.com - I want to get the output HTML source and for PHP to read it, so I can find a string of code in the source. How do I do this?

    I am using cURL to get the page and I can display the page... but I don't know how to get the source code.

    Please help! Thanks.
     
    Helado, Jul 24, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    I'm not familiar with cURL, but you can do something like this in file_get_contents:
    
    <?php
    header('Content-Disposition: attachment; filename="lol.txt"'); // 'lol.txt' contains html source
    echo file_get_contents("http://google.com/");
    ?>
    
    PHP:
     
    Rainulf, Jul 24, 2010 IP
  3. Andrew J

    Andrew J Peon

    Messages:
    32
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $code = file_get_contents('http://google.com');
    
    PHP:
    And then you'd just have to do a preg_match() on $code.
     
    Andrew J, Jul 24, 2010 IP
  4. netload

    netload Member

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #4
    $file = fopen("file.txt", "w");
    
    $ch = curl_init("http://site.com/page.php");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FILE, $file);
    curl_exec($ch);
    curl_close($ch);
    
    fclose($file);
    PHP:
     
    netload, Jul 24, 2010 IP
  5. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    <?php
    $ch = curl_init("http://location.us"); // Start cURL session
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // No output to client, allows storing content in a string instead
    $resource = curl_exec($ch); // Execure cURL session and store contents as a variable
    curl_close($ch); // Close cURL session
    echo $resource; // Send contents to user
    preg_match("!Google!",$resource,$match); // Use contents in a regex and so on
    ?>
    PHP:
     
    iAreCow, Jul 24, 2010 IP
  6. Helado

    Helado Peon

    Messages:
    696
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    I can do that already - I can already get the code and display it. However, I want the HTML source - for example, I can get "Hello my name is bob!" from the page, but I want to get the HTML code: <strong>Hello my name is bob!</strong> - do you see?
     
    Helado, Jul 24, 2010 IP
  7. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Something like this?
    echo '<textarea rows="20" cols="70">';
    echo $result;
    echo '</textarea>';
    PHP:
     
    iAreCow, Jul 24, 2010 IP
  8. Helado

    Helado Peon

    Messages:
    696
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey,

    I sorted it now - I had tried preg_match to get something but it wouldn't work because the code coming back was not what I expected. All good now though, my mistake! Thanks though that would've worked. :)
     
    Helado, Jul 24, 2010 IP
  9. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Okay then! :)
     
    iAreCow, Jul 24, 2010 IP
  10. Alexandros1

    Alexandros1 Peon

    Messages:
    332
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    n HTML a comment's main purpose is to serve as a note to you, the web developer or to others who may view your website's source code. However, PHP's comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the PHP file for editing. This makes PHP comments only useful to PHP programmers.
     
    Alexandros1, Jul 25, 2010 IP