62 lines
1.7 KiB
PHP
62 lines
1.7 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);
|
|
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);
|
|
return json_decode($result);
|
|
}
|
|
}
|