The following codes are from and codes are working well.

This is an example of using snoopy in codeigniter.

Q1. Am I correct to say that I can't use,

$this -> load -> library('snoopy')

since Snoopy.php does not create an object. And the example below is the way to do it? If so, can you explain/direct me an tutorial or explanation of how to do it in details?

if ( ! class_exists('Snoopy'))

Q2. Why do the author use


Is it needed for this?

Q3. Could you explain APPPATH and EXT.


I checked it in but I could not find it. EXT must be extension, but can I use anywhere?

Thanks in advance.

I have a snoopy in application/library/Snoopy.php

I have application/library/Snoopy.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scraping{
    var $c; 
    function Scraping(){
        if ( ! class_exists('Snoopy'))
        $this -> c = new Snoopy();

    function getWebHtml($url="",$to_specialchars=true){
        $this ->c -> fetch( $url );
        $str = mb_convert_encoding( (string) $this -> c -> results,"UTF-8","auto");
        return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8" ) : $str ;

   function getWebText($url="",$to_specialchars=true){
        $this -> c -> fetchtext( $url );
        $str = mb_convert_encoding( (string) $this -> c -> results,"UTF-8","auto");
        return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8" ) : $str ;

    function getWebLinks($url=""){
        $this -> c -> fetchlinks( $url );
        return (array) $this-> c -> results ;

    function getWebLinksText($url="",$delimiter="<br>"){
        $arr = $this-> getWebLinks($url) ;
        $ret ="";
        foreach($arr as $k => $v){
            $ret .= $v . $delimiter ;
        return $ret;

} //endofclass

/* End of file Scraping.php */
/* Location: ./application/libraries/Scraping.php */

I have a controller application/controller/mytasklist.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mytasklist extends Controller {

function Mytasklist()
  parent :: Controller(); 

  $this -> load -> helper( 'url' ); 


    function index()
      $data = "";

      $this -> _SetTpl( $data );
 function _SetTpl( $data )

  $this -> load -> library("scraping");
  $data["scraping"]["text"] = $this-> scraping -> getWebText("");
  $data["scraping"]["html"] = $this-> scraping -> getWebHtml("");
  $data["scraping"]["link"] = $this-> scraping -> getWebLinksText("","\n");

  $tpl["page_title"] = "Welcome";

  $tpl["main_content"] = $this -> load -> view( 'tasklist_view', $data , true ); 

  $this -> load -> view( 'base_view', $tpl );


And I have a view, application/view/base_view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""&gt;
<html xmlns="" xml:lang="ja">
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="keywords" content="keyword here" />
    <meta name="description" content="description here" />
    <title><?php if(isset($page_title)){echo $page_title ;}?></title>
    <?php if(isset($xajax_js)){echo $xajax_js ;}?>
    <link href="" rel="stylesheet" type="text/css"/>

<div id="container">

    <div id="rightblock">

        <div id="content">





+1  A: 

Q1. You can use:


In your controllers. And create a new instance like so:

$snooper = new Snoopy();

The reason they are using:

if (!class_exists('Snoopy')) {

Is because you will get a fatal error if you try and use $this->load->library(), since the loader class is not available in the library. You can call it in a controller is because your controllers extend the controller class, which extends the ci_base class, which extends the ci_loader class which is where the functionality to make calls like $this->load comes from. The Scraping class that you've shown here does not. If you dig down you'll see that the loader is basically using include_once to include whatever class, helper etc. you're trying to use.


$to_specialchars = true

is being used in a couple the function declarations as parameters. Setting it '=true' is just setting a default, so you could can do this:

echo $scrappy->getWebHtml('');

Which is identical to this:

echo $scrappy->getWebHtml('', true);

If you look at the return statement of that function, you'll see they are $to_specialchars is being checked, and if it's true, then the output is run through the PHP function htmlspecialchars() first.

Q3. If you look at the root of your codeigniter project, in index.php you'll see EXT defined as:

define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));


if (is_dir($application_folder))
define('APPPATH', $application_folder.'/');
    if ($application_folder == '')
        $application_folder = 'application';
    define('APPPATH', BASEPATH.$application_folder.'/');

So these are two constants being set at bootstrapping, so you can use them in your application, and if you were to ever change them, then it wouldn't instances like where you see it being used in the code you provided.

Please next time have one question per stackoverflow question :)

You wrote "Is because they are using it in another library that is independent of codeigniter"? How do you know that?
I tried to clear up what I said in Q1. Also the reason I know these things is through reading the code and trying stuff. If you ask one question at a time it is easier to give more thorough answer then one giant answer. Hope this has helped.

Hi! . This sample Scraping code was written based on using the library: "Snoopy - the PHP net client ( )"

I tried to post it again. but I couldn't post with hyperlinks. sorry.. I'll answer to that in my site.(I'm a newbie :-( )

I think that I'll try to repost these answers after a few days .

( )