Is there a way in PHP to "grab" the source of a web page?

10 replies
PROBLEM SOLVED- THANKS
Is there a (relatively simple) way in PHP to "grab" the source code of a web page (by URL) so that you can search it for text?

I want to access a web page's source code and confirm (or not) that a particular piece of code is present.

I want basically copy the manual process of:

opening a web page in the browser
choosing view page source
Find command "some text"
Get a true or false result.

All and any help gratefully received

Tony
#grab #page #php #source #web
  • Profile picture of the author michael_gourlay
    Yes, this is pretty easy:

    $content = file_get_contents("http://www.example.com/something.html");
    {{ DiscussionBoard.errors[3382776].message }}
  • Profile picture of the author carrot
    <?php
    $stringtofind = "whatever you are looking for";
    $content = file_get_contents("http://www.domain.com/page.html");
    $pos = strpos($content,$stringtofind);

    if($pos === false) {
    // Not found
    }
    else {
    // Found
    }
    ?>
    {{ DiscussionBoard.errors[3385931].message }}
    • Profile picture of the author Tony Marriott
      Originally Posted by michael_gourlay View Post

      Yes, this is pretty easy:

      = file_get_contents("http://www.example.com/something.html");
      Thanks Micheal,
      I was Googling around for ages trying to find the command. Normally I don't have any problms finding this kind of stuff but for some reason I just could not pin it down. I thought it must be pretty straight forward.

      Many many Thanks

      Originally Posted by carrot View Post

      <?php
      = "whatever you are looking for";
      = file_get_contents("http://www.domain.com/page.html");
      = strpos(,);

      if( === false) {
      // Not found
      }
      else {
      // Found
      }
      ?>
      Fantastic a complete solution. Exactly what I was after

      You guys Rock!
      {{ DiscussionBoard.errors[3386137].message }}
      • Profile picture of the author jminkler
        Originally Posted by Tony Marriott View Post

        Thanks Micheal,
        I was Googling around for ages trying to find the command. Normally I don't have any problms finding this kind of stuff but for some reason I just could not pin it down. I thought it must be pretty straight forward.

        Many many Thanks



        Fantastic a complete solution. Exactly what I was after

        You guys Rock!
        Some hosting providers will not have this enabled, and you must use the curl solution.

        $Curl = curl_init();
        curl_setopt($Curl, CURLOPT_URL, "https://example.com/");
        curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
        $Data = curl_exec($Curl);
        {{ DiscussionBoard.errors[3386620].message }}
        • Profile picture of the author womki
          I would use curl even if file_get_contents is enabled on your server, curl is faster, much more flexible and you can send custom http headers.
          {{ DiscussionBoard.errors[3387285].message }}
        • Profile picture of the author hilhilginger
          Originally Posted by jminkler View Post

          Some hosting providers will not have this enabled, and you must use the curl solution.

          = curl_init();
          curl_setopt(, CURLOPT_URL, "https://example.com/");
          curl_setopt(, CURLOPT_RETURNTRANSFER, 1);
          = curl_exec();
          I am looking for a solution where i can use cURL function to connect to my remote smtp host.Do you have built in code with written on cURL?
          Signature

          They have over 2300 Offers, Instant PayPal Payments and Free Training Articles.
          {{ DiscussionBoard.errors[3392543].message }}
  • Profile picture of the author Emilis Strimaitis
    You can fetch the source code with file_get_contents(); function, and you can check if the code line is present with strpos();

    Here's the complete script sollution:

    <?

    // Enter the URL of the page that you want to view the source (replace page.com)

    $page = file_get_contents("http://www.page.com");

    // Enter code that you are looking for here (replace code)

    $search = "code";

    // Script will check if code is present on the source code of the URL

    $check = strpos($page,$search);

    // Post the results

    if($check == true)
    {
    echo "The code is found!";
    }
    else
    {
    echo "The code is not found!";
    }

    ?>

    P.S. Sorry I cannot wrap it in CODE tags, because code tags will remove the dollar variables, and script will get messy to understand...
    {{ DiscussionBoard.errors[3388518].message }}
    • Profile picture of the author Tony Marriott
      Many thanks guys for all your help and advice. I now have exactly what I needed up and runnuing and its saving a me a mountain of time.

      I have used the PHP solution not the cURL solution. Simply because I understod teh PHP solution.

      Never seen cURL before and looked lie I would need a little time to understand it so speed and simplicity won the day.

      This is only a rough working model so when I eventualy get someone to do the job properly I'll let them worry about the coding

      Thanks again guys
      Problem solved.
      {{ DiscussionBoard.errors[3389316].message }}
      • Profile picture of the author jminkler
        Originally Posted by Tony Marriott View Post

        Many thanks guys for all your help and advice. I now have exactly what I needed up and runnuing and its saving a me a mountain of time.

        I have used the PHP solution not the cURL solution. Simply because I understod teh PHP solution.

        Never seen cURL before and looked lie I would need a little time to understand it so speed and simplicity won the day.

        This is only a rough working model so when I eventualy get someone to do the job properly I'll let them worry about the coding

        Thanks again guys
        Problem solved.
        You can use the curl functions in php, that is what I posted.

        And just a side note,
        be sure you don't do something like

        file_get_contents($_GET['url']);

        This will open up so many security holes on your site your eyes will pop out

        that is the reason it is disabled on so many hosting providers
        {{ DiscussionBoard.errors[3399764].message }}

Trending Topics