调度中调用的代码删除表中的记录但不添加

时间:2017-05-29 03:53:17

标签: php laravel-5 cron scheduler

我的控制器内核就像这样

 protected function schedule(Schedule $schedule)
{
    $schedule->call('\App\Http\Controllers\HomeController@automatic')->everyMinute();
}

当我打电话给控制器时

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();
    }    
}

删除表中的记录。但是,当我打电话给控制器有这个代码时

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');
}
}

它在我的数据库表中没有写任何内容。当我回显变量时,它会发送数据。 当我手动调用它时,此代码工作正常。

我的cron标签是

php-cli -q /home/allnewsnepal/public_html/artisan schedule:run

1 个答案:

答案 0 :(得分:0)

Laravel调度程序是基于命令行的,你不能在那里使用session和Auth组件

下面的代码在这里没有任何意义

   $user = Auth::user();

您需要将用户信息存储在内存数据库中的其他内容数据库中,如redis,然后使用它