我正在使用Guzzle访问外部API:
$api = new Client([
'base_uri' => 'https://www.space-track.org',
'cookies' => true,
]);
$api->post('ajaxauth/login', [
'form_params' => [
'identity' => 'myidentity',
'password' => 'mypassword',
],
]);
$response = $api->get('basicspacedata/query/class/boxscore/format/json');
return json_decode($response->getBody()->getContents());
$api->get('ajaxauth/logout');
dd($boxscore);
有了这个,我可以访问API并在我的网页上显示JSON文件。我不想这样做,而是将JSON结果保存到我的数据库中,然后从我自己的数据库中显示它。
我能够播种本地JSON文件,但我无法保存Guzzle结果。
是否有将Guzzle响应JSON文件保存到我的数据库中?
修改 以下是数据库的模型和迁移:
移植
Schema::create('operators', function (Blueprint $table) {
$table->increments('id');
$table->string('COUNTRY');
$table->string('SPADOC_CD');
$table->string('ORBITAL_TBA');
$table->string('ORBITAL_PAYLOAD_COUNT');
$table->string('ORBITAL_ROCKET_BODY_COUNT');
$table->string('ORBITAL_DEBRIS_COUNT');
$table->string('ORBITAL_TOTAL_COUNT');
$table->string('DECAYED_PAYLOAD_COUNT');
$table->string('DECAYED_ROCKET_BODY_COUNT');
$table->string('DECAYED_DEBRIS_COUNT');
$table->string('DECAYED_TOTAL_COUNT');
$table->string('COUNTRY_TOTAL');
});
模型
protected $fillable = [
'COUNTRY',
'SPADOC_CD',
'ORBITAL_TBA',
'ORBITAL_PAYLOAD_COUNT',
'ORBITAL_ROCKET_BODY_COUNT',
'ORBITAL_DEBRIS_COUNT',
'ORBITAL_TOTAL_COUNT',
'DECAYED_PAYLOAD_COUNT',
'DECAYED_ROCKET_BODY_COUNT',
'DECAYED_DEBRIS_COUNT',
'DECAYED_TOTAL_COUNT',
'COUNTRY_TOTAL'
];
编辑2: 关于这个问题,我收到了一些帮助。我能够创建一个镜像数据库的模型:
$api = new Client([
'base_uri' => 'https://www.space-track.org',
'cookies' => true,
]); $api->post('ajaxauth/login', [
'form_params' => [
'identity' => 'myidentity',
'password' => 'mypassword',
],
]);
$response = $api-get('basicspacedata/query/class/boxscore/format/json');
$mydata = json_decode($response->getBody()->getContents());
$object = new Object();
$object->COUNTRY = $mydata->COUNTRY;
$object->SPADOC_CD = $mydata->SPADOC_CD
$object->save();
return redirect('your/url',compact('object'));
这会从数据库中提取数据,但不保存。它允许我访问已保存的变量。
我需要做的是以某种方式将变量保存到我的卫星中。数据库中。
TL; DR:我只是想将从API获取的数据保存到我的数据库
答案 0 :(得分:0)
我认为@ D.777KLM的意思是,他(我也是)正试图接触女巫,并将响应的每个键都保存到数据库中。
我的嘴巴回应是:
{
"atividade_principal": [
{
"text": "Formação de condutores",
"code": "85.99-6-01"
}
],
"data_situacao": "25/02/2001",
"nome": "AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA",
"uf": "SP",
"telefone": "(17) 3258-1649 / (17) 3258-1649",
"email": "mccontab1@yahoo.com.br",
"qsa": [
{
"qual": "49-Sócio-Administrador",
"nome": "ELIANA CRISTINA GUILHERMITTI RODRIGUES"
},
{
"qual": "49-Sócio-Administrador",
"nome": "SUZANA MARQUES LOBANCO"
},
{
"qual": "49-Sócio-Administrador",
"nome": "AMARILDO APARECIDO RODRIGUES"
},
{
"qual": "22-Sócio",
"nome": "VALDEMIR FRANCISCO DA COSTA"
},
{
"qual": "49-Sócio-Administrador",
"nome": "ANDRE LUIS LOBANCO"
}
],
"situacao": "ATIVA",
"bairro": "CENTRO",
"logradouro": "R CASTRO ALVES",
"numero": "1408",
"cep": "15.115-000",
"municipio": "BADY BASSITT",
"porte": "MICRO EMPRESA",
"abertura": "02/06/1995",
"natureza_juridica": "206-2 - Sociedade Empresária Limitada",
"cnpj": "00.635.344/0001-77",
"ultima_atualizacao": "2019-01-03T10:43:40.334Z",
"status": "OK",
"tipo": "MATRIZ",
"fantasia": "",
"complemento": "",
"efr": "",
"motivo_situacao": "",
"situacao_especial": "",
"data_situacao_especial": "",
"atividades_secundarias": [
{
"code": "00.00-0-00",
"text": "Não informada"
}
],
"capital_social": "1000.00",
"extra": [],
"billing": {
"free": true,
"database": true
}
}
我的模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use GuzzleHttp\Client;
class CNPJ extends Model
{
static function getCNPJ($cnpj)
{
$client = new Client();
$response = $client->request('GET', 'https://www.receitaws.com.br/v1/cnpj/' . $cnpj);
$dadosReceita = $response->getBody()->getContents();
return json_decode($dadosReceita, true);
}
}
在控制器上:
$dadosReceita = CNPJ::getCNPJ($cnpj);
return $dadosReceita;
例如,如何获取"natureza_juridica": "206-2 - Sociedade Empresária Limitada"
并将其保存?
我感觉非常愚蠢,因为这似乎很容易而且很基本。