You are here

17 May 2016

Class for fetching and sending data between directo.ee API. This class includes pulling data from Directo and posting xml with php array to xml generation.

Source code viewer
  1. <?php
  2. /**
  3.  * @file Directo import class.
  4.  *
  5.  * @author Ra Mänd <ram4nd@gmail.com>
  6.  * @link http://browse-tutorials.com/
  7.  * @tutorial http://browse-tutorials.com/snippet/directo-class
  8.  *
  9.  * Example:
  10.  * require_once 'directo.php'
  11.  * $directo = new directo('[https://directo.gate.ee/xmlcore/TYPE/xmlcore.asp]', '[Your Key]');
  12.  * $response = $directo->curl_get_xml_object(
  13.  * 'what',
  14.  * date('d.m.Y', date('yesterday'))
  15.  * );
  16.  */
  17. class directo
  18. {
  19. private $url;
  20. private $key;
  21. public $errors = array();
  22. public function __construct($url, $key) {
  23. $this->url = $url;
  24. $this->key = $key;
  25. }
  26. /**
  27.   * @param $what
  28.   * @param $ts
  29.   *
  30.   * @return SimpleXMLElement
  31.   */
  32. public function curl_get_xml_object($what, $ts) {
  33. 'get' => 1,
  34. 'what' => $what,
  35. 'ts' => $ts,
  36. 'key' => $this->key,
  37. )));
  38. // Curl request to receive the xml.
  39. $ch = curl_init();
  40. curl_setopt($ch, CURLOPT_URL, $this->url . '?' . $query);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($ch, CURLOPT_HEADER, 0);
  43. $response = curl_exec($ch);
  44. curl_close($ch);
  45. return simplexml_load_string($response);
  46. }
  47. /**
  48.   * @param $what
  49.   * @param $xml_array
  50.   *
  51.   * @return void
  52.   */
  53. public function curl_post($what, $xml_array) {
  54. $xml =
  55. '<?xml version="1.0" encoding="utf-8"?>'."\n".
  56. $this->_format_xml_elements($xml_array);
  57. 'put' => 1,
  58. 'what' => $what,
  59. 'xmldata' => $xml,
  60. ), null, '&');
  61. // Curl request to receive the xml.
  62. $ch = curl_init();
  63. curl_setopt($ch, CURLOPT_URL, $this->url);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  65. curl_setopt($ch, CURLOPT_HEADER, 0);
  66. curl_setopt($ch, CURLOPT_POST, 1);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  68. $this->_error(curl_exec($ch));
  69. curl_close($ch);
  70. }
  71. private function _error($msg) {
  72. $this->errors[] = $this->url . ' - ' . date('d.m.Y H:i:s') . ': ' . $msg;
  73. }
  74. private function _format_xml_elements($array) {
  75. $output = '';
  76. foreach ($array as $key => $value) {
  77. if (is_numeric($key)) {
  78. if ($value['key']) {
  79. $output .= ' <' . $value['key'];
  80. if (isset($value['attributes']) && is_array($value['attributes'])) {
  81. $output .= $this->_attributes($value['attributes']);
  82. }
  83. if (isset($value['value']) && $value['value'] != '') {
  84. $output .= '>' . (is_array($value['value']) ? $this->_format_xml_elements($value['value']) : $this->_check_plain($value['value'])) . '</' . $value['key'] . ">\n";
  85. }
  86. else {
  87. $output .= " />\n";
  88. }
  89. }
  90. }
  91. else {
  92. $output .= ' <' . $key . '>' . (is_array($value) ? $this->_format_xml_elements($value) : $this->_check_plain($value)) . "</$key>\n";
  93. }
  94. }
  95. return $output;
  96. }
  97. private function _attributes(array $attributes = array()) {
  98. foreach ($attributes as $attribute => &$data) {
  99. $data = implode(' ', (array) $data);
  100. $data = $attribute . '="' . $this->_check_plain($data) . '"';
  101. }
  102. return $attributes ? ' ' . implode(' ', $attributes) : '';
  103. }
  104.  
  105. private function _check_plain($text) {
  106. return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
  107. }
  108. }
  109.  
Programming Language: PHP