I am using Laravel 5.8 in my server. I have this external endpoint:
It is a GET Request and in JSON the format as shown below.
[
{
"ClientID": "C001",
"ClientName": "ABU PLC",
"Address": "ADDRESS LINE 1",
"City": "Lagos"
},
"ClientID": "C002",
"ClientName": "ADE PLC",
"Address": "ADDRESS LINE 1",
"City": "Lagos"
},
"ClientID": "C003",
"ClientName": "JACOB PLC",
"Address": "ADDRESS LINE 1",
"City": "Lagos"
}
]
Then, in my server I have this table:
CREATE TABLE `customers` (
`id` int NOT NULL,
`client_id` varchar(255) UNIQUE NOT NULL,
`client_name` varchar(255) UNIQUE NOT NULL,
`address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The model class is Customer.
client_id
and client_name
are unique and should be reference points.
I want to save all the data in the endpoint at the same time in my Customer model class.
It should update where client_id
and client_name
already exists but insert where not
ClientID
into client_id
, ClientName
into client_name
, Address
into address
I have something like this, but don't know how to continue:
public function apiclient(Request $request)
{
$array_content=[];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.abclm.net/customerss/allcustomers");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Important
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$i=0;
foreach(...)
{
$newobj = new Clients();
....
array_push($array_content,$newobj);
$i++;
}
return $array_content;
}
I have something like this but don't know how to go about it. Kindly assist.
Instead of using manual curl, I find this Guzzle wrapper easier. http://docs.guzzlephp.org/en/stable/
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.abclm.net/customerss/allcustomers');
return $res->getBody();