Enable curl Support
$curl = $curl = $curl = $curl = $curl = $curl = $curl Extension =php_curl. DLL extension=php_curl.
The controller
/ / parameters
public function difu($type,$page,$page_size,$is_filter){
$param=input();
/ / type
if(isset($params['type']) &&! empty($params['type'])){
$type=$params['type'];
}
/ / page
if(isset($params['page']) &&! empty($params['page'])){
$page=$params['page'];
}
// Display the number of entries per page
if(isset($params['page_size']) &&! empty($params['page_size'])){
$page_size=$params['page_size'];
}
// Whether to return news with content details
if(isset($params['is_filter']) &&! empty($params['is_filter'])){
$filter=$params['is_filter'];
}
/ / the secret key
$appid="";
// Aggregate the news URL
$url="http://v.juhe.cn/toutiao/index?"
."type=".$type."&page=".$page."&page_size=".$page_size."&is_filter="
.$is_filter."&key={$appid}";
// $res=file_get_contents($url);
$res=Cache::get('news');
// $data=Cache::set('news',$data);
$newsData=$res['result'] ['data']; News::addNews($newsData); } model layer codestatic public function apicate($category_id){
$data=self::hasWhere('category'.'='.'category_id')->field('id');
}
static public function addNews($data){
$categoryID=Category::where('category_name'.'=',$data[0] ['category'])
->value('id');
if(empty($categoryID)){
$res=Category::create([
'category_name'=>$data[0] ['category']]); $categoryID=$res->id; }self::startTrans();
try{
foreach($data as &$val){
// self::static($val['uniquekey']??'');
self::create([
'title'=>$val['title'].'pubdate_time'=>strtotime($val['date']),
'category_id'=>$categoryID,
'news_url'=>$val['url']????' '.'author_name'=>$val['author_name']????' '.'thumbnail_pic_s'=>$val['thumbnail_pic_s']????' '.'thumbnail_pic_s02'=>$val['thumbnail_pic_s02']????' '.'thumbnail_pic_s03'= >! empty($val['thumbnail_pic_s03'])??' '.'is_content'=>$val['is_content']]); }self::commit();
return ;
}catch(\Exception $e){
self::rollback();
throw $e;
}
// $result=(new News())->saveall($data,true);
}
Copy the code
Detailed content collection
$url = 'https://mini.eastday.com/mobile/210609182318170808361.html';
// Define the collection rule
$rules = [
// Collect article titles
'title'= > ['h1'.'text'].'img'= > ['img'.'src'].// Collect article authors
'author'= > ['span'.'text'].// Collect the content of the article
'content'= > ['p'.'text']]. $rt = QueryList::get($url)->rules($rules)->query()->getData();// print_r($rt); die();
$arr=$rt->all();
if(Newss::create($arr)){
return 'Added successfully';
}else{
return 'Add failed';
}
print_r($rt);
Copy the code
Frequent Interface requests
public function redis(){
$redis = new Redis();
$ip = $_SERVER['REMOTE_ADDR'];
$ipData = $redis->get($ip);
$ipExpireTime = $redis->ttl($ip);
if ($ipExpireTime<=0){
$ipExpireTime = 60;
}
// Check whether the request is malicious if it is not null
if(! empty($ipData)) {if ($ipData >= 10) {
return writeJson(10003.' '.'Please don't ask too often!! ');
}
}
$params = input();
// News list
// print_r($params); die();
$result = News::select($params);
// Save the number of IP requests
$i = 1;
$redis->set($ip, $ipData + $i, $ipExpireTime);
return writeJson(0, $result, 'Operation successful');
}
Copy the code