Is there any web-page copying command in PHP?

by 20 replies
23
What is that command in PHP that will allow me to copy
an html code of a web page by its URL?
#programming #command #copying #php #webpage
  • CURL allows that. Check here:

    Grab HTML content with CURL | drupal.org
    • [1] reply
    • Hello techymarketer!

      Thank you for that link. I am just surprized why curl is not
      mentioned in any php tutorial that I have read.
      • [1] reply
  • If fopen wrappers is enabled in your php, you can the file_get_contents() function to get the html code.

    For example,

    $str = file_get_contents("hxxp://somedomain.com");
    echo $str;
    • [1] reply
    • xga,
      can you, please, tell me what is wrappers and how I can enable it?
      • [1] reply
  • Create a php file that has this in it:

    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    Open that up in your browser and you should see a bunch of information about PHP. Search for the following text: allow_url_fopen - if the "Local Value" column says "On", then you can use the following methods to get the code of a page: (this isn't an exhaustive list, just a few examples)

    Method 1: (easiest)


    <?php
    $html = file_get_contents('http://url.here/');
    ?>


    Method 2:


    <?php
    $f = fopen('http://url.here/', 'r');
    $html = '';
    while ($line = fread($f, 1024))
    {
    $html .= $line;
    }
    fclose();
    ?>


    Method 3:


    <?php
    $html = file('http://url.here');
    ?>


    Note about method 3: $html will be an array with each element of the array being one line in the code. For example:


    $html = array(
    [0] => "<html>",
    [1] => "<head>",
    [2] => "<title>Website title here</title>",
    and so on
    );
  • OK seriously, we can't show a dollar sign in code tags?
    • [1] reply
  • Excellent explaination of file_get_contents etc.

    Once you've got the HTML code you probably want to do something with it. In this case take a look at Simple HTML DOM -

    PHP Simple HTML DOM Parser

    With this awesome library you can parse and extract parts of the code, and deal with the HTML code from the page as a PHP object. Very useful!
    • [1] reply
    • Thanks, but, before parsing any code, I need to know what
      kind of script I could run on a free remote server, so that that script
      would periodically open the desired web pages and store their
      code somewhere on the server or at least mail it to my mail box.

      Believe it or not, but, after asking so many questions on forums, I
      still haven't figured out how to do it. It seems to me that there are
      only three possible ways:

      1) ;
      2) ;
      3) ;

      In, as you can see in this thread, I stumbled on the problem
      of not being able to even open any because on free servers
      the "" value is not enabled (well, I was also advised
      here to use with , but I haven't tried it yet; I am afraid
      is good for performing processes on your own computer, but not on
      any free remote server);

      In I was able to go further - I was able to get a script that
      would open s and display pages, but alas, I can't automatically
      create or even open any files there, as free servers, being afraid
      of viruses, disable those functions, too. Right now I am working on
      how to get the opened code emailed to me in .

      As for way, I haven't started to learn it yet.
  • For security purposes, it's best that you use curl and set allow_url_fopen to off in your php.ini
  • I've search on google, and found some tutorial using SnoopyPHP.
    Hope it can help you.

    Rainbow Hat SEO PHP4: How to Steal from Yahoo! (another Web Screen Scraper)
    Rainbow Hat SEO PHP5: Screen scraping with DOM and XPath
    • [1] reply

Next Topics on Trending Feed