mcatze
Well-known member
Hi everybody,
i have a public function with an API call. Everytime the api call is made, i want to set an log entry. This works fine, but i have no the issue that after every call the log entry is doubled in the database. I couldn't find out what's the problem. I do not have the call twice in the code.
Has someone a hint what is wrong or where i can take a look? Its my first XF2 add-on and in the learning phase..
This is the function i build:
i have a public function with an API call. Everytime the api call is made, i want to set an log entry. This works fine, but i have no the issue that after every call the log entry is doubled in the database. I couldn't find out what's the problem. I do not have the call twice in the code.
Has someone a hint what is wrong or where i can take a look? Its my first XF2 add-on and in the learning phase..
This is the function i build:
PHP:
public function fetchLocationData($location)
{
$serviceID = 'geocode';
$address = $location;
$params = [
'address' => $address,
];
$apiUrl = $this->fetchService($serviceID, $params);
/**
* @var \GuzzleHttp\Client $client
*/
$client = \XF::app()->http()->client();
$geocodeResponse = $client->get($apiUrl)->getBody();
$geocodeData = \GuzzleHttp\json_decode($geocodeResponse);
/** @var \XT\Membermap\XF\Entity\User $visitor */
$visitorId = \XF::visitor()->user_id;
$logData = [
'user_id' => $visitorId,
'request_url' => $apiUrl,
'request_status' => $geocodeData->status,
'request_data' => $geocodeData->results,
];
$requestLog = $this->em()->create('XT\Membermap:Log');
$requestLog->bulkSet($logData);
$requestLog->save();
if (
!empty($geocodeData)
&& $geocodeData->status == 'OK'
&& isset($geocodeData->results)
&& isset($geocodeData->results[0])
) {
return [
'latitude' => $geocodeData->results[0]->geometry->location->lat,
'longitude' => $geocodeData->results[0]->geometry->location->lng
];
}
}