| Server IP : 103.48.194.25 / Your IP : 216.73.216.242 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/hocthongminh.net-bk/public_html/webadmin/components/robot/libraries/ |
Upload File : |
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
include_once("simple_html_dom.php");
class robot
{
var $html;
var $html_content = '';
var $arr_att_clean = array();
function robot(){
$this->html = new simple_html_dom();
}
function Dom(){
return $this->html;
}
function getHTML($link)
{
if($this->html_content==''){
$html = file_get_html($link);
$this->html_content = $html;
}else{
$html = $this->html_content;
}
return $html ; //return object
}
function getCurl($link)
{
//Begin connect
$ch = curl_init($link);
if (!$ch){
//$this->setErr(RB_ERR_NOT_INIT_CURL);
//$this->setErr($url);
//return false;
}
// Chúng ta sẽ nhận trị trả về và nhận dưới dạng binary
// Dó đó ta phải set 2 option như sau
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
// Nếu không set như trên, bạn sẽ không nhận được gì
// khi curl được thực thi, tức giá trị NULL
// run curl and save value into data
$html = curl_exec($ch);
// close connect
curl_close($ch);
//End connect
return $html ; //return boolean
}
public function getCurlMuti($nodes)
{
$mh = curl_multi_init();
for($i=0;$i<count($nodes);$i++)
{
$url = $nodes[$i];
$curl_array[$i] = curl_init($url);
curl_setopt($curl_array[$i], CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6");
//curl_setopt($curl_array[$i], CURLOPT_URL, $nodes[$i]);
curl_setopt($curl_array[$i], CURLOPT_HEADER, true);
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_array[$i], CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl_array[$i], CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_array[$i], CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl_array[$i], CURLOPT_TIMEOUT,30);
curl_multi_add_handle($mh, $curl_array[$i]);
}
$running = NULL;
do {
//usleep(10000);
curl_multi_exec($mh,$running);
} while($running > 0);
for($i=0;$i<count($nodes);$i++)
{
$data[$i]['content'] = curl_multi_getcontent($curl_array[$i]);
}
for($i=0;$i<count($nodes);$i++)
{
curl_multi_remove_handle($mh, $curl_array[$i]);
}
curl_multi_close($mh);
return $data;
}
public function multiRequest($data, $options = array())
{
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
//*******************************************
function getBlock($content, $rule)
{
$this->html->load($content);
$arr = array();
foreach($this->html->find($rule) as $e)
array_push($arr, $e->innertext);
return $arr;
}
function getNodes($content, $rule, $num=-1)
{
$this->html->load($content);
$arr = array();
if($num==-1)
foreach($this->html->find($rule) as $e)
array_push($arr, $e->innertext);
else
foreach($this->html->find($rule,$num) as $e)
array_push($arr, $e->innertext);
return $arr;
}
function getLink($content, $rule, $num=-1)
{
$this->html->load($content);
$arr = array();
if($num==-1)
foreach($this->html->find($rule) as $e)
array_push($arr, $e->href);
else
foreach($this->html->find($rule,$num) as $e)
array_push($arr, $e->href);
return $arr;
}
function removeLink($content)
{
$html = str_get_html($content);
// link content
foreach($html->find('a') as $e){
$e->outertext = $e->innertext;
}
$ret = $html->save();
return $ret;
}
function removeLastElement($content, $element)
{
$html = str_get_html($content);
// link content
$html->find($element, -1)->outertext = '';
$ret = $html->save();
return $ret;
}
function removeFirstElement($content, $element)
{
$html = str_get_html($content);
$html->find($element, 0)->outertext = '';
$ret = $html->save();
return $ret;
}
}
?>