1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP tutorial

Discussion in 'PHP & Perl' started by toots, Feb 1, 2012.

  1. toots

    toots Newbie

    Joined:
    Feb 1, 2012
    Messages:
    14
    Likes Received:
    9
    Occupation:
    Click Merchant
    Location:
    Where the sun doesn't always shine
    Anyone know of a good PHP tutorial for a beginner?

    My immediate goal is to scrape a website (3000 pages) daily, parse the html for specific data, and put the data into a database/spreadsheet.

    I have done a lot of googling and I've seen people recommend DOM with XMLReader.

    Any advice on how to accomplish this, or just general advice on how to start with PHP is greatly appreciated.

    I know it takes a lot of time and work to learn anything, but I'm prepared to do it.

    Thanks.
     
  2. GfxDude

    GfxDude Registered Member

    Joined:
    Jan 15, 2012
    Messages:
    67
    Likes Received:
    98
    Location:
    ლ(ಠ益ಠლ)
    look into w3schools
    they have some pretty sweet tuts on php and all that

    u can also look into this guys channel video programming tutorials :)
    thenewboston <-- youtube it

    or some Php in 21 days :rolleyes:
     
    Last edited: Feb 1, 2012
  3. shubhamm

    shubhamm Junior Member

    Joined:
    Jan 25, 2010
    Messages:
    107
    Likes Received:
    25
    Occupation:
    Developer
    Location:
    BHW
    @toots

    i dont think PHP is write Lang. For this type work why not try Another lang. which is not web based ??


    Or if you still want to learn PHP then as GfxDude is saying.. the yt channel of thenewboston or his site is great Resources to be a Pro. in PHP
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    I mostly use Zend Framework 's classes for scraping things. I 'm posting some of my code that 's easy to read and will help you (or anyone) start with web scraping. :)

    Study it and make it yours, especially the second part.

    Here 's my grabber class, it returns the HTML code and stores the time it took to grab it and the HTTP response code (e.g. 200, 404 etc). See Zend_Http for details.

    PHP:
    class Networking
    {
        public 
    $pageLoadTime 0;
        public 
    $respstatus;

        function 
    getPage($url)
        {
            
    $client = new Zend_Http_Client($url, array('maxredirects' => 2'timeout' => 10,
                
    'useragent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.3) Gecko/20010801 '));
            
    $time_start microtime(true);
            
    $code $client->request("GET");
            
    $time_end microtime(true);
            
    $time $time_end $time_start;
            
    $this->pageLoadTime $time;
            
    $this->respstatus $code->getStatus();

            return 
    $code;
        }

    }
    After the HTML code has been fetched, I create the DOM document with Zend_Dom

    Here 's my class for scraping Google Blogs

    PHP:
    <?php

    class BlogResults
    {
        public 
    $url;
        public 
    $anchor;
    }

    class 
    GoogleBlogSearch
    {
        public 
    $pageBody '';
        public 
    $pageDom '';

        function 
    getPage($url)
        {
            
    $url noWWW(noHTTP($url));
            
    $worker = new Networking();
            
    $page "http://blogsearch.google.com/blogsearch?hl=en&q=" noVars($url);
            
    $code $worker->getPage($page);
            
    $this->pageBody $code->getBody();
            
    $this->pageDom = new Zend_Dom_Query($this->pageBody);
        }

        function 
    parseResults()
        {
            
    $results $this->pageDom->query('a');
            if (
    count($results)) {
                
    $tmp_links = array();
                foreach (
    $results as $result) {
                    if (!
    $this->validLink($result->getattribute("href")))
                        continue;
                    if (
    in_array($result->getattribute("href"), $tmp_links))
                        continue;
                    
    $tmp = new BlogResults();
                    if (
    $result->hasattribute("href"))
                        
    $tmp->url $result->getattribute("href");
                    
    $tmp->anchor $result->textContent;
                    
    $tmp_links[] = $tmp->url;
                    
    $ret[] = $tmp;
                }
            }

            return 
    $ret;
        }

        function 
    validLink($url)
        {
            if (
    preg_match('#google\.#'$url)) {
                return 
    false;
            }
            if (!
    preg_match('#http:\/\/#'$url)) {
                return 
    false;
            }
            if (
    preg_match('#youtube\.#'$url)) {
                return 
    false;
            }
            return 
    true;
        }
    }

    ?>
    Enjoy :)
     
    • Thanks Thanks x 3
    Last edited: Feb 1, 2012
  5. x.Ampit.x

    x.Ampit.x Junior Member

    Joined:
    Jun 16, 2011
    Messages:
    114
    Likes Received:
    27
    you can also try youtubedotcom/phpacademy if you want to start learning php.

    There's some good tut's out there.

    And As GfxDude said thenewboston channel also have some good php tutorials for beginners.
     
  6. blackhatchan

    blackhatchan Registered Member

    Joined:
    Jan 7, 2012
    Messages:
    51
    Likes Received:
    8
    I think PHP for dummies it's good...
     
  7. marsgibson

    marsgibson Junior Member

    Joined:
    Aug 1, 2011
    Messages:
    109
    Likes Received:
    17


    should i use these codes too? hope you wont mind... i have been looking out for long time to grab some php tutorials and to learn some stuff and improve my web skills.. found finally one!! thanks dude :)
     
  8. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Well, it goes without saying that of course you can :) That 's why it 's posted on the forum, so that everyone can freely use it if they find it useful for what they do ;)

    Keep in mind that it 's not copy&paste and run code. It 's posted as reference to your learning procedure. For example, you can see there 's an utility function called noWWW() which is not defined anywhere in that code. Just remove it or re-implement it or let me know and I 'll dig it up and send it over.