PHP tutorial

toots

Newbie
Joined
Feb 1, 2012
Messages
14
Reaction score
9
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.
 
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:
@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
 
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 (0 < 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 :)
 
Last edited:
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.
 
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 Code:
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 Code:
<?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 (
0 < 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 :)



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 :)
 
should i use these codes too? hope you wont mind...

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.
 
Back
Top