Webservice.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace WebserviceBundle\Services;
  3. use Buzz\Message\Request as HttpRequest;
  4. use Buzz\Message\RequestInterface as HttpRequestInterface;
  5. use Buzz\Message\Response as HttpResponse;
  6. class Webservice
  7. {
  8. protected $serviceContainer;
  9. protected $securityTokenStorage;
  10. protected $httpClient;
  11. public function __construct($serviceContainer, $securityTokenStorage, $httpClient)
  12. {
  13. $this->serviceContainer = $serviceContainer;
  14. $this->securityTokenStorage = $securityTokenStorage;
  15. $this->httpClient = $httpClient;
  16. }
  17. /**
  18. * @param string $webservice
  19. * @param array $params
  20. * @return array
  21. */
  22. public function getChoices($webservice, $params = array())
  23. {
  24. $choices = array();
  25. $results = $this->getArray($webservice, $params);
  26. foreach ($results as $row) {
  27. $choices[$row['name']] = $row['id'];
  28. }
  29. return $choices;
  30. }
  31. /**
  32. * @param string $webservice
  33. * @param array $params
  34. * @return array
  35. */
  36. public function getArray($webservice, $params = array())
  37. {
  38. $results = array();
  39. if ($this->serviceContainer->hasParameter($webservice)) {
  40. $url = $this->serviceContainer->getParameter($webservice);
  41. $url .= '?filters[qb-criteria]';
  42. foreach ($params as $param => $value) {
  43. $url .= "&filters[{$param}]=$value";
  44. }
  45. try {
  46. $response = $this->makeGetRequest($url);
  47. $results = json_decode($response->getContent(), true);
  48. } catch (\Exception $ex) {
  49. }
  50. }
  51. return $results;
  52. }
  53. /**
  54. * @param string $url
  55. * @return HttpResponse
  56. */
  57. public function makeGetRequest($url)
  58. {
  59. $response = '';
  60. if ($token = $this->securityTokenStorage->getToken()) {
  61. $accessToken = $token->getAccessToken();
  62. $request = new HttpRequest(HttpRequestInterface::METHOD_GET, $url);
  63. $response = new HttpResponse();
  64. $request->setHeaders(array('Authorization: Bearer ' . $accessToken));
  65. $this->httpClient->send($request, $response);
  66. }
  67. return $response;
  68. }
  69. /**
  70. * @param string $webservice
  71. * @param array $filters
  72. * @param array $order_by
  73. * @param integer $limit
  74. * @param integer $offset
  75. * @return array
  76. */
  77. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  78. {
  79. if ($this->serviceContainer->hasParameter($webservice)) {
  80. $url = $this->serviceContainer->getParameter($webservice) . "?";
  81. if($filters) {
  82. $url .= http_build_query(array('filters' => $filters));
  83. }
  84. if($order_by) {
  85. $url .= http_build_query(array('order_by' => $order_by));
  86. }
  87. if($limit) {
  88. $url .= "&limit={$limit}";
  89. }
  90. if($offset) {
  91. $url .= "&offset={$offset}";
  92. }
  93. $data = array();
  94. try {
  95. $data = json_decode(file_get_contents($url), true);
  96. } catch (\Exception $ex) {
  97. }
  98. //$data['url'] = $url;
  99. return $data;
  100. }
  101. return array();
  102. }
  103. /**
  104. * @param string $webservice
  105. * @param int $id
  106. * @return string
  107. */
  108. public function getById($webservice, $id)
  109. {
  110. $result = $this->getArray($webservice, array(
  111. 'id' => $id
  112. ));
  113. return isset($result[0]) ? "{$result[0]['id']} - {$result[0]['name']}" : $id;
  114. }
  115. /**
  116. * @param string $webservice
  117. * @param array $filters
  118. * @param array $order_by
  119. * @param integer $limit
  120. * @param integer $offset
  121. * @return array
  122. */
  123. public function putData($webservice, $id, $data)
  124. {
  125. if ($this->serviceContainer->hasParameter($webservice)) {
  126. $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  127. $data_string = json_encode($data);
  128. $ch = curl_init($url);
  129. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  130. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  131. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($data_string)));
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  133. try {
  134. $return = curl_exec($ch);
  135. } catch (\Exception $ex) {
  136. return array("error"=>"Connection Error.");
  137. }
  138. if($return) {
  139. return json_decode($return, true);
  140. } else {
  141. return array("error"=>"Transaction Error.");
  142. }
  143. }
  144. return array("error"=>"Webservice({$webservice}) not found.");
  145. }
  146. }