Small Grabber demo based on OOP

Started on the old forum in February 2012 · view the original in the archive · old links redirect here

#class #demo #grabber #oop

No replies · 896 views · started 14 years ago · 1 min read

just_m3. Developer · 199 posts #1

We Got 2 files for use

grab_lib.php && index.php

OOP BASED SCRIPT : grab_lib.php

<?php  
class grabber{

var $grab;

function page($url,$mode){

//empty ? // fallin' back to file_get_contents
if (empty($mode)){$mode='fget';} 

if ($mode=='fget'){

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "User-Agent: SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)\r\n"
  )
);

$context = stream_context_create($opts);

// Send HTTP headers set above thiss allow us to set browser model
$page=file_get_contents($url, false, $context);
$ret = $this->grab=$page;
}

if ($mode=='curl'){
$crl = curl_init();
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl,CURLOPT_USERAGENT,'SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)');
//Make sure its sent , send user agent via header to 
curl_setopt ($crl, CURLOPT_HTTPHEADER,array('User-Agent: SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html),'));
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
$grab = curl_exec($crl); curl_close($crl);
$page=$grab;
$ret = $this->grab=$page;
}

//echo 'Using '.$mode.' fetching contents';
return $ret; }

}

?>

CLASSIC PHP : index.php

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Here is my link grabber demo</title>
</head>

<body>
<?php
###############################
require_once'grab_lib.php';   #
###############################

$grab = new grabber();

########### FILE LINKS ONLY ############ 

$moddmydata = $grab->page('http://whatmp3.name/search.php?query=house+base','curl');

// make local links 
$moddmydata = str_replace("http://whatmp3.name/","",$moddmydata);
// remove all div's on page 
$moddmydata = preg_replace('/<div(.*?)>/is','',$moddmydata);
$moddmydata = str_replace('</div>','',$moddmydata);
// remove all scripts on page 
$moddmydata = preg_replace('/<script(.*?)<\/script>/is','',$moddmydata);
// remove all images
$moddmydata = preg_replace('/<img(.*?)>/is','',$moddmydata);
// here you can mod the links
$moddmydata = preg_replace('/<a href="download.php(.*?)">/is','<a href="my_file.php$1&act=download">',$moddmydata); 
// remove all ads links ussualy starts with http:// 
//site links should be local pointing like /file.php?file=12453
$moddmydata = preg_replace('/<a href="http:\/\/(.*?)<\/a>/is','',$moddmydata); 
$moddmydata = preg_replace("/(.*?)<a href=\"(.*?)<\/a>/is","<a href=\"$2</a></br>\n",$moddmydata); 
$moddmydata = preg_replace("/<!--(.*?)<\/html>/is","",$moddmydata);
echo $moddmydata;
?>

</body>
</html>

you can use this class for easy switching between grabbing methods file_get_contents (fget) and curl both methods implemented with user agent .

class attached bellow

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

Attachments: grab_lib.zip

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close