dimanche 28 mai 2017

Code called in schedular deletes the records in table but doesnt add

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 use

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'=>"http://ift.tt/2qr0GVn",
                        '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'=>"http://ift.tt/2qr0GVn",
                        '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://ift.tt/2rbtvCp','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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2r3aTpK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire