Hi all please bare with me I am still learning Laravel/Lumen.
I have created a Lumen API and created a controller to import a companies details from the Companies House API to my database.
I have used Guzzle to get the data from Companies House and now I can't figure out how to take the different parts of the json response and add it to my database. Here is my Code
Route web.php
$router->group(['prefix' => 'v1'], function () use ($router) {
/**
* Company Routes
*/
$router->get('/company/import','CompanyController@importCompany');
});
CompanyController
namespace App\Http\Controllers;
use App\Companies;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class CompanyController extends Controller {
public function __construct() {
//
}
public function importCompany(Request $request) {
$chapikey = env('CH_API_KEY');
$client = new \GuzzleHttp\Client();
$response = $client->get('https://api.companieshouse.gov.uk/company/11422512', [
'auth' => [
$chapikey , ''
]
]);
return $response->getBody()->getContents();
}
}
The Companies House API uses basic auth with my api key which is in my env file as CH_API_KEY and uses a null password.
This successfully grabs my companies official government record and returns it as text/html even though it is json.
The API's reply is:
{
"links": {
"filing_history": "/company/11422512/filing-history",
"self": "/company/11422512",
"officers": "/company/11422512/officers",
"persons_with_significant_control": "/company/11422512/persons-with-significant-control"
},
"company_name": "THE PEOPLE RANGE LTD",
"company_status": "active",
"has_charges": false,
"accounts": {
"next_due": "2020-03-19",
"accounting_reference_date": {
"day": "30",
"month": "06"
},
"next_made_up_to": "2019-06-30",
"last_accounts": {
"type": "null"
},
"next_accounts": {
"period_start_on": "2018-06-19",
"overdue": false,
"period_end_on": "2019-06-30",
"due_on": "2020-03-19"
},
"overdue": false
},
"jurisdiction": "england-wales",
"confirmation_statement": {
"overdue": false,
"next_made_up_to": "2019-06-18",
"next_due": "2019-07-02"
},
"has_insolvency_history": false,
"company_number": "11422512",
"registered_office_is_in_dispute": false,
"date_of_creation": "2018-06-19",
"registered_office_address": {
"country": "United Kingdom",
"postal_code": "HU9 2NS",
"locality": "Hull",
"address_line_1": "6 Middleton Avenue"
},
"type": "ltd",
"sic_codes": [
"94990"
],
"etag": "b1ffcb83ad5c524b546ebc890d989b90d8407e63",
"undeliverable_registered_office_address": false,
"can_file": true
}
So I atleast know that side is working. What I need to do is extract parts of that response and prepare it for eloquent to be able to add it to my database. If it helps my database matches the json keys for the parts I want to store.
Any help would be apreciated.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community