使用Laravel任务计划请求URL

时间:2016-10-20 23:01:49

标签: php laravel curl laravel-5.1

环境

  • Laravel版本: 5.1.45(LTS)

  • PHP版本: 5.6.1

描述

我正试图每分钟都跑一条路。

尝试

我试过

$schedule->exec('curl '.env('APP_URL').'fbwifi/acl_update')->everyMinute()
        ->sendOutputTo(public_path().'/tasks/log.txt');

Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->exec('curl '.env('APP_URL').'fbwifi/acl_update')->everyMinute()
        ->sendOutputTo(public_path().'/tasks/log.txt');
    }
}

我所做的是对路线进行GET的最佳方式吗?还是有更好的方式来研究?

我现在正在接受任何建议。

任何提示/建议/帮助都将非常感谢!

2 个答案:

答案 0 :(得分:3)

使用可能的解决方案进行更新:

如果你的控制器在默认命名空间中,那么你可以在调度的调用函数中使用完整的命名空间路径来使用它。

默认命名空间: App \ Http \ Controllers

假设 fbwifi 是您的控制器&amp; acl_update 是您的方法。

$schedule->call('App\Http\Controllers\fbwifi@acl_update')
         ->everyMinute()
         ->sendOutputTo(public_path().'/tasks/log.txt');

我想通过创建单独的类来实现最佳实践,如下文所述。

https://www.sitepoint.com/managing-cronjobs-with-laravel/

1)创建命令类 - &gt;提供命令的签名和说明

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sms:birthday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';


2)将handle()方法定义到该命令类中

public function handle()
{
  User::whereBirthDate(date('m/d'))->get(); 

  foreach( $users as $user ) {
    if($user->has('cellphone')) {
    SMS::to($user->cellphone)
       ->msg('Dear ' . $user->fname . ', I wish you a happy birthday!')
       ->send();
    }   
}  

$this->info('The happy birthday messages were sent successfully!');

}


3)在ConsoleKernal扩展类的$ commands数组中添加该命令类

protected $commands = [
     // ...
     'App\Console\Command\HappyBirthday'
];


4)根据您的要求安排您的命令,如下所示。

protected function schedule(Schedule $schedule)
{   
  $schedule->command('sms:birthday')->daily();
}

答案 1 :(得分:1)

首先,当你问一个类似“如何从另一个调用一个控制器函数”这样的问题时,你应该重新访问你的代码来分离任务特定的逻辑并将其提取到另一个类文件中。然后你可以使用新课程,并在任何你想要的地方调用它,就像在cron工作中一样。

由于您正在寻找普通的旧卷曲请求功能,因此在辅助文件中创建函数非常方便

function http_response($url, $status = null, $wait = 3) 
{ 
    $time = microtime(true); 
    $expire = $time + $wait; 

    // we fork the process so we don't have to wait for a timeout 
    $pid = pcntl_fork(); 
    if ($pid == -1) { 
        die('could not fork'); 
    } else if ($pid) { 
        // we are the parent 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_HEADER, TRUE); 
        curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        $head = curl_exec($ch); 
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 

        if(!$head) 
        { 
            return FALSE; 
        } 

        if($status === null) 
        { 
            if($httpCode < 400) 
            { 
                return TRUE; 
            } 
            else 
            { 
                return FALSE; 
            } 
        } 
        elseif($status == $httpCode) 
        { 
            return TRUE; 
        } 

        return FALSE; 
        pcntl_wait($status); //Protect against Zombie children 
    } else { 
        // we are the child 
        while(microtime(true) < $expire) 
        { 
        sleep(0.5); 
        } 
        return FALSE; 
    } 
} 

但是如果你想要使用Object和API,那么你可能想要使用它 http://docs.guzzlephp.org/en/latest/quickstart.html

如果你没有作曲家

curl -sS https://getcomposer.org/installer | php
php composer.phar require guzzlehttp/guzzle

如果您已经全局安装了作曲家

composer require guzzlehttp/guzzle

然后你可以发出请求

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// 200
echo $res->getHeaderLine('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});
$promise->wait();

并提出其他类型的请求,例如

$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');