发布于2023-05-25 21:39 阅读(1084) 评论(0) 点赞(29) 收藏(0)
My controller kernel is like this
protected function schedule(Schedule $schedule)
{
$schedule->call('\App\Http\Controllers\HomeController@automatic')->everyMinute();
}
When i call the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Storage;
use App\news;
use Auth;
use DOMDocument;
use Exception;
class HomeController extends Controller
{
public function automatic()
{
function delete(){
DB::table('news')->delete();
echo "table deletted";
}
delete();
}
}
It deletes the records in the table. But when i call controller having this code
class HomeController extends Controller
{
public function automatic()
{
function follow_links_reportersnepal($url,$cat)
{
ini_set('max_execution_time', 9000);
global $already_crawled;
global $crawling;
$i=0;
$doc = new DOMDocument();
@$doc->loadHTML(@file_get_contents($url));
$linklist = $doc->getElementsByTagName("a");
$already_crawled[]="sailaab";
foreach ($linklist as $link)
{
try {
$l = $link->getAttribute("href");
if(strlen($l)==45)
{
if (!in_array($l, $already_crawled))
{
$i++;
if ($i>2) {
break;
}
$already_crawled[] = $l;
$content = file_get_contents($l);
$first_step = explode( '<h3 class="display-4">' , $content);
$second_step = explode('</h3>' , $first_step[1]);//title
$third_step=explode('<div class="entry-content">',$second_step[1]);
$fourth_step=explode('<p>',$third_step[1]);
$fifth_step=explode('<div class="at-below-post', $fourth_step[1]);
$sixth_step=explode('<figure class="figure">', $content);
if(isset($sixth_step[1])){
$seventh_step=explode('src="', $sixth_step[1]);
$eighth_step=explode('"', $seventh_step[1]);
$url = $eighth_step[0];
$img=rand();
$img=(string)$img;
file_put_contents($img, file_get_contents($url));
$user = Auth::user();
news::create([
'news_title'=>strip_tags($second_step[0]),
'category_id'=>$cat,
'source_id'=>'reportersnepal',
'reference_url'=>"www.reportersnepal.com",
'reference_detail'=>$l,
'news_summary'=>"null",
'news_detail'=>strip_tags($fifth_step[0]),
'news_image'=>$img,
'news_video'=>"null",
'news_status'=>"1",
'created_by'=>$user->id,
'last_updated_by'=>$user->id,
]);
}
else{
$user = Auth::user();
news::create([
'news_title'=>strip_tags($second_step[0]),
'category_id'=>$cat,
'source_id'=>'reportersnepal',
'reference_url'=>"www.reportersnepal.com",
'reference_detail'=>$l,
'news_summary'=>"null",
'news_detail'=>strip_tags($fifth_step[0]),
'news_image'=>"default.png",
'news_video'=>"null",
'news_status'=>"1",
'created_by'=>$user->id,
'last_updated_by'=>$user->id,
]);
}
}
}
} catch (Exception $e) {
continue;
}
}
}
follow_links_reportersnepal('http://reportersnepal.com/category/featured','1');
}
}
It doesnt write anything in my database table. When i echo the variables it dispays the data. This code works fine when i call them manually.
And my cron tab is
php-cli -q /home/allnewsnepal/public_html/artisan schedule:run
Laravel scheduler are command line based and you cannot use session and Auth components there
Your code below dont make any sense here
$user = Auth::user();
You need to store user information on some other in memory database like redis
and then use it
作者:黑洞官方问答小能手
链接:http://www.phpheidong.com/blog/article/546160/5fc9e35e0058698ee70f/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!