使用Laravel任务调度请求URL


Request URL using Laravel Task Scheduling

环境

  • Laravel版本:5.1.45(LTS(

  • PHP版本:5.6.


说明

我每分钟都在努力找到一条特定的路线。


尝试

我试过

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

我所做的是到达路线的最佳方式吗?还是有更好的方法我应该调查?

更新可能的解决方案:

若您的控制器在默认的名称空间中,那个么您可以通过在schedule的调用函数中使用全名称空间路径来使用它。

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

假设fbwifi是您的控制器&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( 创建命令类->提供命令的签名和描述

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

首先,当你问一个问题,比如"如何从另一个控制器调用一个控制器函数,你应该重新访问你的代码,以分离特定于任务的逻辑,并将其提取到不同的类文件中。然后,你可以使用新的类,并在任何你想要的地方调用它,比如在cron作业中。

由于您希望制作Plain旧的curl请求函数,因此在助手文件中创建一个函数非常方便

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

但如果你想拥有一些对象和API,那么你可能想使用http://docs.guzzlephp.org/en/latest/quickstart.html

如果你没有作曲家

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

如果您已经在全球范围内安装了composer

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