Hướng dẫn lấy tin khuyến mãi, khuyến mại website hc.com.vn (ok)
https://hc.com.vn/ords/home
Bản ok
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="http://localhost/library/owlcarousel2/normalize.css">
<link rel="stylesheet" type="text/css" href="http://localhost/library/owlcarousel2/owl.carousel.min.css">
<script type="text/javascript" src="http://localhost/library/jquery/jquery.min.js"></script>
<script type="text/javascript" src="http://localhost/library/owlcarousel2/owl.carousel.min.js"></script>
</head>
<body>
<?php
include 'simple_html_dom.php';
class GetDataForWeb {
private $url, $first, $second;
public function __construct($link, $class1 = '#best_selling .hc-best-selling-desc a', $class2 = '#content .large-image img',$class3='#pdb-ajax h1 span', $addlink1 = '', $addlink2 = '') {
$this->url = $link;
$this->first = $class1;
$this->second = $class2;
$this->five = $class3;
$this->three = $addlink1;
$this->four = $addlink2;
}
private function getDom($base) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
$html_base = new simple_html_dom();
$html_base->load($str);
return $html_base;
}
private function checkUrl($url, $bool = false) {
if (strpos($url, 'https://') === 0 || strpos($url, 'http://') === 0) {
return $url;
} else {
if ($bool) {
return $this->four . $url;
} else {
return $this->three . $url;
}
}
}
public function getList() {
$dom = $this->getDom($this->url);
?>
<div class="carouselinfocompany owl-carousel owl-theme">
<?php
foreach ($dom->find($this->first) as $href) {
?>
<div class="item">
<a href="<?php echo $this->checkUrl($href->href); ?>">
<img src="<?php echo $this->getListInner($this->checkUrl($href->href)); ?>" alt="aaaaaaaa">
<div class="titles">
<?php
$domsecond = $this->getDom($this->checkUrl($href->href));
echo $domsecond->find($this->five,0)->innertext;
?>
</div>
</a>
</div>
<?php
}
?>
</div>
<?php
}
public function getListInner($url = '') {
$dominner = $this->getDom($url);
return $this->checkUrl($dominner->find($this->second,0)->src,true);
}
}
$aGetDataForWeb = new GetDataForWeb('https://hc.com.vn/ords/home', '#best_selling .hc-best-selling-desc a', '#content .large-image img','#content .title-product h1', 'https://hc.com.vn/ords/', 'https://hc.com.vn');
// $aGetDataForWeb = new GetDataForWeb('https://mediamart.vn/', '.home-pl-promotion a', '#pd-slideproduct .item img','#pdb-ajax h1 span', 'https://mediamart.vn/', 'https://mediamart.vn/');
$aGetDataForWeb->getList();
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.carouselinfocompany').owlCarousel({
loop:true,
margin:10,
nav:true,
});
});
</script>
<style>.item a {text-decoration: none;} .item .title {text-align: center;margin-top: 15px;}</style>
</body>
</html>
Bản trước :)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="http://localhost/library/owlcarousel2/normalize.css">
<link rel="stylesheet" type="text/css" href="http://localhost/library/owlcarousel2/owl.carousel.min.css">
<script type="text/javascript" src="http://localhost/library/jquery/jquery.min.js"></script>
<script type="text/javascript" src="http://localhost/library/owlcarousel2/owl.carousel.min.js"></script>
</head>
<body>
<?php
include 'simple_html_dom.php';
class GetDataForWeb {
private $url, $first, $second;
public function __construct($link, $class1 = '#best_selling .hc-best-selling-desc a', $class2 = '#content .large-image img', $addlink1 = '', $addlink2 = '') {
$this->url = $link;
$this->first = $class1;
$this->second = $class2;
$this->three = $addlink1;
$this->four = $addlink2;
}
private function getDom($link) {
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
$dom = str_get_html($content);
return $dom;
}
private function checkUrl($url, $bool = false) {
if (strpos($url, 'https://') === 0 || strpos($url, 'http://') === 0) {
return $url;
} else {
if ($bool) {
return $this->four . $url;
} else {
return $this->three . $url;
}
}
}
public function getList() {
$dom = $this->getDom($this->url);
?>
<div class="carouselinfocompany owl-carousel owl-theme">
<?php
foreach ($dom->find($this->first) as $href) {
?>
<div class="item">
<a href="<?php echo $this->checkUrl($href->href); ?>">
<img src="<?php echo $this->getListInner($this->checkUrl($href->href)); ?>" alt="aaaaaaaa">
<div class="title"><?php echo $href->innertext; ?></div>
</a>
</div>
<?php
}
?>
</div>
<?php
}
public function getListInner($url = '') {
$dominner = $this->getDom($url);
return $this->checkUrl($dominner->find($this->second,0)->src,true);
}
}
$aGetDataForWeb = new GetDataForWeb('https://hc.com.vn/ords/home', '#best_selling .hc-best-selling-desc a', '#content .large-image img', 'https://hc.com.vn/ords/', 'https://hc.com.vn');
$aGetDataForWeb->getList();
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.carouselinfocompany').owlCarousel({
loop:true,
margin:10,
nav:true,
});
});
</script>
<style>.item a {text-decoration: none;} .item .title {text-align: center;margin-top: 15px;}</style>
</body>
</html>
PreviousSửa lỗi Simple html dom file_get_html not working - is there any workaround? (ok)NextTìm hiểu về Simple HTML Dom (ok)
Last updated
Was this helpful?