Posted By

knedle on 10/10/12


Tagged

php ares


Versions (?)

ARES


 / Published in: PHP
 

trida na ziskani dat z aresu

  1. /**
  2.  * prekopana class>
  3.  * http://blog.katuscak.cz/clanek/ziskani-dat-z-registru-ares/
  4.  * od @author Michal KatuÃ��Ã��Ã�¡k <[email protected]>
  5.  * @license Creative Commons 3.0
  6.  * http://blog.katuscak.cz/clanek/ziskani-dat-z-registru-ares/
  7.  *
  8.  * @author Ladislav Sevcuj
  9.  * - navazano na jine url - basic
  10.  * - ziskan vetsi objedm dat
  11.  */
  12. class ARES {
  13.  
  14. /** @var string */
  15. private $ares_url = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=';
  16.  
  17. /** @var int */
  18. private $ic;
  19.  
  20. /**
  21.   * @param int $ic IÃ�� of subject
  22.   */
  23. public function __construct($ic) {
  24. $this->ic = (int) $ic;
  25. }
  26.  
  27. /**
  28.   * @return array Data
  29.   */
  30. public function getData() {
  31. $url = $this->ares_url . $this->ic;
  32. $xml = $this->getXML($url);
  33. if ($this->ic == $this->getIcFromXML($xml)) {
  34. return array(
  35. 'name' => $this->getNameFromXML($xml),
  36. 'street' => $this->getStreetFromXML($xml),
  37. 'street_number' => $this->getStreetNumberFromXML($xml),
  38. 'city' => $this->getCityFromXML($xml),
  39. 'psc' => $this->getPscFromXML($xml),
  40. 'ic' => $this->ic,
  41. 'dic' => $this->getDicFromXML($xml),
  42. 'type' => $this->getTypeFromXML($xml),
  43. 'type_string' => $this->getTypeStringFromXML($xml),
  44. 'country' => $this->getCountryFromXML($xml),
  45. );
  46. }
  47. return false;
  48. }
  49.  
  50. /**
  51.   * @param string $xml
  52.   * @return string
  53.   */
  54. private function getDicFromXML($xml) {
  55. $pattern = '/<D:DIC zdroj="DPH">([A-Z0-9]*)<\/D:DIC>/';
  56. preg_match($pattern, $xml, $matches);
  57. return isset($matches[1]) ? $matches[1] : '';
  58. }
  59.  
  60. /**
  61.   * @param string $xml
  62.   * @return string
  63.   */
  64. private function getTypeFromXML($xml) {
  65. $pattern = '/<D:KPF>([0-9]{3})<\/D:KPF>/';
  66. preg_match($pattern, $xml, $matches);
  67. if (isset($matches[1])) {
  68. return ($matches[1] >= 111) ? 'PO' : 'FO';
  69. }
  70. return false;
  71. }
  72.  
  73. /**
  74.   * @param string $xml
  75.   * @return string
  76.   */
  77. private function getIcFromXML($xml) {
  78. $pattern = '/<D:ICO zdroj="(RZP|OR)">([0-9]*)<\/D:ICO>/';
  79. preg_match($pattern, $xml, $matches);
  80. return isset($matches[2]) ? $matches[2] : '';
  81. }
  82.  
  83. /**
  84.   * @param string $xml
  85.   * @return string
  86.   */
  87. private function getNameFromXML($xml) {
  88. $pattern = '/<D:OF zdroj="(OR|RZP)">(.*)<\/D:OF>/';
  89. preg_match($pattern, $xml, $matches);
  90. return isset($matches[2]) ? $matches[2] : '';
  91. }
  92.  
  93. /**
  94.   * @param string $xml
  95.   * @return string
  96.   */
  97. private function getStreetFromXML($xml) {
  98. $pattern = '/<D:NU>(.*)<\/D:NU>/';
  99. preg_match($pattern, $xml, $matches);
  100. return isset($matches[1]) ? $matches[1] : '';
  101. }
  102.  
  103. /**
  104.   * @param string $xml
  105.   * @return string
  106.   */
  107. private function getStreetNumberFromXML($xml) {
  108. $pattern = '/<D:CD>(.*)<\/D:CD>/';
  109. preg_match($pattern, $xml, $matches);
  110. return isset($matches[1]) ? $matches[1] : '';
  111. }
  112.  
  113. /**
  114.   * @param string $xml
  115.   * @return string
  116.   */
  117. private function getCityFromXML($xml) {
  118. $pattern = '/<D:N>(.*)<\/D:N>/';
  119. preg_match($pattern, $xml, $matches);
  120. return isset($matches[1]) ? $matches[1] : '';
  121. }
  122.  
  123. /**
  124.   * @param string $xml
  125.   * @return string
  126.   */
  127. private function getPscFromXML($xml) {
  128. $pattern = '/<D:PSC>(.*)<\/D:PSC>/';
  129. preg_match($pattern, $xml, $matches);
  130. return isset($matches[1]) ? $matches[1] : '';
  131. }
  132.  
  133. /**
  134.   * @param string $xml
  135.   * @return string
  136.   */
  137. private function getTypeStringFromXML($xml) {
  138. $pattern = '/<D:NPF>(.*)<\/D:NPF>/';
  139. preg_match($pattern, $xml, $matches);
  140. return isset($matches[1]) ? $matches[1] : '';
  141. }
  142.  
  143.  
  144. /**
  145.   * @param string $xml
  146.   * @return string
  147.   */
  148. private function getCountryFromXML($xml) {
  149. $pattern = '/<D:NS>(.*)<\/D:NS>/';
  150. preg_match($pattern, $xml, $matches);
  151. return isset($matches[1]) ? $matches[1] : '';
  152. }
  153.  
  154. /**
  155.   * @param string $url
  156.   * @return string
  157.   */
  158. private function getXML($url) {
  159. return file_get_contents($url);
  160. }
  161.  
  162. }
  163.  
  164. // spusteni
  165.  
  166. /**
  167.  * naÃ��tenÃ�½ dat z aresu
  168.  * http://blog.katuscak.cz/clanek/ziskani-dat-z-registru-ares/
  169.  */
  170. $ic = intval($_GET['ic']);
  171.  
  172. $data = null;
  173.  
  174. if (!empty($ic)) {
  175.  
  176. require_once dirname(__FILE__) . '/../../lib/ares.php';
  177.  
  178. $ares = new ARES($ic); // Zad�¡te I��
  179. $data = $ares->getData(); // Z�­sk�¡te array s daty
  180. }
  181.  
  182. echo json_encode($data);

Report this snippet  

You need to login to post a comment.