使用Guzzle在Laravel中使用外部API并保存到数据库中

使用Guzzle在Laravel中使用外部API并保存到数据库中,laravel,api,guzzle,Laravel,Api,Guzzle,我正在我的服务器中使用Laravel-5.8。我拥有无法更改的外部端点: 它是一个GET请求,采用JSON(应用程序/JSON)格式 然后,在我的服务器中有以下表格: CREATE TABLE `students` ( `id` int NOT NULL auto_increment, `registration_no` varchar(255) UNIQUE NOT NULL, `full_name` varchar(255) UNIQUE NOT NULL, `student_class

我正在我的服务器中使用Laravel-5.8。我拥有无法更改的外部端点:

它是一个GET请求,采用JSON(应用程序/JSON)格式

然后,在我的服务器中有以下表格:

 CREATE TABLE `students` (
`id` int NOT NULL auto_increment,
`registration_no` varchar(255) UNIQUE NOT NULL,
`full_name` varchar(255) UNIQUE NOT NULL,
`student_class` varchar(255) NOT NULL,
`academic_term` varchar(255) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;
我正在编写一个cron作业,它将使用Guzzle将外部api保存到我的数据库中

我查看了下面的网站,阅读了一些信息,但一路上我被卡住了


  • 为Laravel计划创建cron作业
  • crontab-e

    ***cd/[laravel项目路径]&php artisan计划:运行>>/dev/null 2>&1

  • 将console命令添加到console/kernel.php

  • 对于
    updateOrCreate()
    方法,有几个简单的修复方法可以帮助您找到解决方案

    首先,可以使用大括号在PHP中将字符串作为属性传递。当出现诸如空格之类的问题时,或者您希望动态地传递带有字符串值的变量时,这会很方便

    /$client->{$string}
    $client->{'Full Name'}
    
    其次,
    updateOrCreate()
    接受的不是一个数组,而是两个数组。第一个用于搜索现有条目。如果
    注册号
    与现有条目匹配,则以下示例将更新数据库,如果不匹配,则创建新条目

    Student::updateOrCreate([
    “注册号”=>$client->{'Student Code'},
    //'full_name'=>$client->{'full name'},//取消注释以同时使用这两个名称
    ],
    [
    'full_name'=>$client->{'full name'},//如果上面没有注释,则不需要
    “学生类”=>$client->StudentClass,
    “学术术语”=>$client->academic术语
    ]);
    
    编辑:
    如果要执行
    注册号
    全名
    ,您可能必须在更新前手动搜索值,因为
    updateOrCreate()
    在存在多个字段时使用AND条件进行搜索。

    为什么要使用此
    return$result->getBody()如果使用学生代码作为id如何?所以,使用字符串作为主键使其具有唯一的id
    
     CREATE TABLE `students` (
    `id` int NOT NULL auto_increment,
    `registration_no` varchar(255) UNIQUE NOT NULL,
    `full_name` varchar(255) UNIQUE NOT NULL,
    `student_class` varchar(255) NOT NULL,
    `academic_term` varchar(255) NOT NULL
    
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    <?php
    
    namespace App\Console\Commands;
    use Illuminate\Console\Command;
    
    
    use App\Student;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Log;
    use GuzzleHttp\Exception\GuzzleException;
    use GuzzleHttp;
    use GuzzleHttp\Client;
    
    class studentdataupdate extends Command {
    
     protected $signature = 'command:studentdataupdate';
     protected $description = 'Student Data Update';
     public function __construct() {
            parent::__construct();
        }
    
     public function handle() {  
     $client = new GuzzleHttp\Client();
     $res = $client->request('GET','https://api.studklm.net/students/allstudents');
    
            return $result->getBody(); 
    
            $clients = json_decode($result, true);
    
            foreach($clients as $client) {
                Student::updateOrCreate([
                    'registration_no' => $client->Student Code,
                    'full_name' => $client->Full Name,
                    'student_class' => $client->StudentClass,
                    'facademic_term' => $client->AcademicTerm
                ]);
            }              
        }
    }
    
    
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('command:studentdataupdate')->everyMinute(); 
    }