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

Consume External API in Laravel with Guzzle and Save into the Database

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

https://api.studklm.net/students/allstudents

这是一个 GET 请求,格式为 JSON (application/json)。

[
  {
    "Student Code": "S001",
    "Full Name": "Adeleke Omowale",
    "StudentClass": "JSS 1",
    "AcademicTerm": "First"
  },
    "Student Code": "S002",
    "Full Name": "James Smith",
    "StudentClass": "JSS 1",
    "AcademicTerm": "First"
  },
    "Student Code": "S003",
    "Full Name": "Haruna Muri",
    "StudentClass": JSS 2",
    "AcademicTerm": "First"
  }
]

然后,在我的服务器中我有这个 table:

 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 保存到我的数据库中。

我查看了下面的站点以阅读一些信息,但一路卡住了。

https://appdividend.com/2018/04/17/laravel-guzzle-http-client-example/

<?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
            ]);
        }              
    }
}

我想达到这些结果:

  1. registration_no 和 full_name 是唯一的,不应允许重复

  2. 外部api的一些字段格式不正确,中间有空格。例如,全名学生代码。在尝试无误地保存到我的数据库时如何解决此问题?

  3. 如果全名学生代码已经存在,它会更新。否则,它会插入新记录。

  1. 为 Laravel 计划
  2. 创建一个 cron 作业

crontab -e

* * * * * cd /[path-to-your-laravel-project] && php artisan schedule:run >> /dev/null 2>&1

  1. 将您的控制台命令添加到 console/kernel。php
protected function schedule(Schedule $schedule)
{
     $schedule->command('command:studentdataupdate')->everyMinute(); 
}

您的 updateOrCreate() 方法有一些简单的修复方法,可以帮助您找到解决方案。

首先,您可以使用大括号将字符串作为 PHP 中的属性传递。当出现空格等问题时,或者您想动态传递具有字符串值的变量时,这会派上用场。

// $client->{$string}
$client->{'Full Name'}

其次,updateOrCreate() 接受的不是一个数组,而是两个数组。第一个用于搜索现有条目。如果 registration_no 匹配现有条目,以下示例将更新数据库,如果不匹配,则创建一个新条目。

Student::updateOrCreate([
    'registration_no' => $client->{'Student Code'},
    // 'full_name' => $client->{'Full Name'}, // uncomment to use both
],
[
    'full_name' => $client->{'Full Name'}, // not required if uncommented above
    'student_class' => $client->StudentClass,
    'facademic_term' => $client->AcademicTerm
]);

编辑: 如果您想执行 registration_no OR full_name,您可能必须在更新之前手动搜索值,因为 updateOrCreate() 在存在多个字段时使用 AND 条件进行搜索。