FFXIV_Lodestone_Handler/app/Services/LodestoneApiService.php

65 lines
1.8 KiB
PHP

<?php
namespace App\Services;
use Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class LodestoneApiService
{
public function getCharacterById($id) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/character/'.$id);
return $this->completeCurlRequest($ch);
}
public function getCharacterByName($server, $name) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/character/search?name='.$name.'&server='.$server);
$characterResults = $this->completeCurlRequest($ch);
if ($characterResults->Pagination->Results === 0)
throw new NotFoundHttpException();
return $this->getCharacterById($characterResults->Results[0]->ID);
}
public function getFreeCompanyById($id) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/freecompany/'.$id.'?data=FCM');
return $this->completeCurlRequest($ch);
}
public function getFreeCompanyByName($server, $name) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/freecompany/search?name='.$name.'&server='.$server);
$characterResults = $this->completeCurlRequest($ch);
if ($characterResults->Pagination->Results === 0)
throw new NotFoundHttpException();
return $this->getFreeCompanyById($characterResults->Results[0]->ID);
}
private function completeCurlRequest($ch)
{
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error:' . curl_error($ch));
}
curl_close($ch);
$json = json_decode($result);
if (property_exists($json, 'Error') && $json->Error === true)
return null;
return $json;
}
}