|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Emretnrvrd\Tckn\Services\TcknVerifier; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +class TcknApi |
| 8 | +{ |
| 9 | + private TcknRequest $request; |
| 10 | + |
| 11 | + private const URL = "https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx"; |
| 12 | + private const HTTPHEADER = [ |
| 13 | + 'POST /Service/KPSPublic.asmx HTTP/1.1', |
| 14 | + 'Host: tckimlik.nvi.gov.tr', |
| 15 | + 'Content-Type: text/xml; charset=utf-8', |
| 16 | + 'SOAPAction: "http://tckimlik.nvi.gov.tr/WS/TCKimlikNoDogrula"', |
| 17 | + 'Content-Length: {{length}}' |
| 18 | + ]; |
| 19 | + private const XML_PATTERN = |
| 20 | + '<?xml version="1.0" encoding="utf-8"?> |
| 21 | + <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> |
| 22 | + <soap:Body> |
| 23 | + <TCKimlikNoDogrula xmlns="http://tckimlik.nvi.gov.tr/WS"> |
| 24 | + <TCKimlikNo>{{tckn}}</TCKimlikNo> |
| 25 | + <Ad>{{name}}</Ad> |
| 26 | + <Soyad>{{last_name}}</Soyad> |
| 27 | + <DogumYili>{{birthyear}}</DogumYili> |
| 28 | + </TCKimlikNoDogrula> |
| 29 | + </soap:Body> |
| 30 | + </soap:Envelope> |
| 31 | + '; |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + public function __construct(TcknRequest $request) |
| 36 | + { |
| 37 | + $this->request = $request; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Sends the HTTP request to the TCKN verification API |
| 43 | + * @throws Exception if an error occurs due to the API |
| 44 | + * @return string the API response |
| 45 | + */ |
| 46 | + public function request() |
| 47 | + { |
| 48 | + try { |
| 49 | + $curl = curl_init(); |
| 50 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 51 | + curl_setopt($curl, CURLOPT_POST, true); |
| 52 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 53 | + curl_setopt($curl, CURLOPT_HEADER, FALSE); |
| 54 | + curl_setopt($curl, CURLOPT_URL, self::URL); |
| 55 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getXmlData()); |
| 56 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getHttpHeader()); |
| 57 | + $response = curl_exec($curl); |
| 58 | + curl_close($curl); |
| 59 | + |
| 60 | + return $response; |
| 61 | + } catch (\Exception $e) { |
| 62 | + throw new Exception("An error occurred due to the API."); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates the XML request data for the API request |
| 68 | + * @return string the XML request data |
| 69 | + */ |
| 70 | + private function getXmlData() |
| 71 | + { |
| 72 | + $data = [ |
| 73 | + "{{tckn}}" => $this->request->tckn, |
| 74 | + "{{name}}" => $this->request->name, |
| 75 | + "{{last_name}}" => $this->request->lastname, |
| 76 | + "{{birthyear}}" => $this->request->birthyear, |
| 77 | + ]; |
| 78 | + return str_replace(array_keys($data), array_values($data), self::XML_PATTERN); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates the HTTP header for the API request |
| 83 | + * @return array the HTTP header |
| 84 | + */ |
| 85 | + private function getHttpHeader() |
| 86 | + { |
| 87 | + return str_replace("{{length}}", strlen($this->getXmlData()), self::HTTPHEADER); |
| 88 | + } |
| 89 | +} |
0 commit comments