Webservice.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace WebserviceBundle\Services;
  3. use Buzz\Client\Curl;
  4. use Buzz\Message\Request as HttpRequest;
  5. use Buzz\Message\RequestInterface as HttpRequestInterface;
  6. use Buzz\Message\Response as HttpResponse;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. class Webservice
  10. {
  11. /**
  12. * @var ContainerInterface
  13. */
  14. protected $serviceContainer;
  15. /**
  16. * @var TokenStorage
  17. */
  18. protected $securityTokenStorage;
  19. /**
  20. * @var Curl
  21. */
  22. protected $httpClient;
  23. /**
  24. * @param ContainerInterface $serviceContainer
  25. * @param TokenStorageInterface $securityTokenStorage
  26. * @param Curl $httpClient
  27. */
  28. public function __construct(ContainerInterface $serviceContainer, TokenStorageInterface $securityTokenStorage, Curl $httpClient)
  29. {
  30. $this->serviceContainer = $serviceContainer;
  31. $this->securityTokenStorage = $securityTokenStorage;
  32. $this->httpClient = $httpClient;
  33. }
  34. /**
  35. * @param string $webservice
  36. * @param array $params
  37. *
  38. * @return array
  39. */
  40. public function getChoices($webservice, $params = array())
  41. {
  42. $choices = array();
  43. $results = $this->getArray($webservice, $params);
  44. foreach ($results as $row) {
  45. if (isset($row['name']) && isset($row['id'])) {
  46. $choices[$row['name']] = $row['id'];
  47. }
  48. }
  49. return $choices;
  50. }
  51. /**
  52. * @param string $webservice
  53. * @param array $params
  54. *
  55. * @return array
  56. */
  57. public function getArray($webservice, $params = array())
  58. {
  59. $results = array();
  60. if ($this->serviceContainer->hasParameter($webservice)) {
  61. $url = $this->serviceContainer->getParameter($webservice);
  62. $url .= '?filters[qb-criteria]';
  63. foreach ($params as $param => $value) {
  64. $url .= "&filters[{$param}]=$value";
  65. }
  66. try {
  67. $results = json_decode($this->makeGetRequest($url), true);
  68. } catch (\Exception $ex) {
  69. }
  70. }
  71. return (array)$results;
  72. }
  73. /**
  74. * @param string $url
  75. * @param string $method
  76. *
  77. * @return HttpResponse
  78. */
  79. public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET)
  80. {
  81. $request = new HttpRequest($method, $url);
  82. $response = new HttpResponse();
  83. $this->httpClient->send($request, $response);
  84. return $response->getContent();
  85. }
  86. /**
  87. * @param string $url
  88. * @param string $method
  89. *
  90. * @return HttpResponse
  91. */
  92. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  93. {
  94. $response = '';
  95. if ($token = $this->securityTokenStorage->getToken()) {
  96. $headers = array();
  97. $request = new HttpRequest($method, $url);
  98. if (!empty($data)) {
  99. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  100. $request->setContent(http_build_query($data));
  101. }
  102. $response = new HttpResponse();
  103. if (method_exists($token, 'getAccessToken')) {
  104. $headers[] = 'Authorization: Bearer ' . $token->getAccessToken();
  105. }
  106. $request->setHeaders($headers);
  107. $this->httpClient->send($request, $response);
  108. $response = $response->getContent();
  109. }
  110. return $response;
  111. }
  112. /**
  113. * @param string $webservice
  114. * @param array $filters
  115. * @param array $order_by
  116. * @param integer $limit
  117. * @param integer $offset
  118. *
  119. * @return array
  120. */
  121. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  122. {
  123. if ($this->serviceContainer->hasParameter($webservice)) {
  124. $url = $this->buildUrl($webservice, $filters, $order_by, $limit, $offset);
  125. $data = array();
  126. try {
  127. $data = json_decode($this->makeGetRequest($url), true);
  128. } catch (\Exception $ex) {
  129. }
  130. return $data;
  131. }
  132. return array();
  133. }
  134. /**
  135. * @param string $url
  136. * @param array $filters
  137. * @param array $order_by
  138. * @param integer $limit
  139. * @param integer $offset
  140. *
  141. * @return array
  142. */
  143. public function get($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  144. {
  145. $data = array();
  146. try {
  147. $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
  148. $data = json_decode($this->makeRequest($url), true);
  149. } catch (\Exception $ex) {
  150. }
  151. return $data;
  152. }
  153. /**
  154. * @param string $webservice
  155. * @param array $filters
  156. * @param array $order_by
  157. * @param int $limit
  158. * @param int $offset
  159. *
  160. * @return string
  161. */
  162. public function buildUrl($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  163. {
  164. $url = $webservice . '?';
  165. if ($this->serviceContainer->hasParameter($webservice)) {
  166. $url = $this->serviceContainer->getParameter($webservice) . '?';
  167. }
  168. if ($filters) {
  169. $url .= http_build_query(array('filters' => $filters));
  170. }
  171. if ($order_by) {
  172. $url .= '&' . http_build_query(array('order_by' => $order_by));
  173. }
  174. if ($limit) {
  175. $url .= "&limit={$limit}";
  176. }
  177. if ($offset) {
  178. $url .= "&offset={$offset}";
  179. }
  180. return $url;
  181. }
  182. /**
  183. * @param string $webservice
  184. * @param int $id
  185. *
  186. * @return string
  187. */
  188. public function getById($webservice, $id)
  189. {
  190. $result = $this->getArray($webservice, array(
  191. 'id' => $id
  192. ));
  193. return isset($result[0]) && isset($result[0]['id']) && isset($result[0]['name'])
  194. ? "{$result[0]['id']} - {$result[0]['name']}"
  195. : $id;
  196. }
  197. /**
  198. * @param string $webservice
  199. * @param int $id
  200. * @param array $data
  201. *
  202. * @return array
  203. */
  204. public function putData($webservice, $id, $data)
  205. {
  206. if ($this->serviceContainer->hasParameter($webservice)) {
  207. $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  208. $data_string = json_encode($data);
  209. $ch = curl_init($url);
  210. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  211. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  212. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($data_string)));
  213. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  214. try {
  215. $return = curl_exec($ch);
  216. } catch (\Exception $ex) {
  217. return array("error"=>"Connection Error.");
  218. }
  219. if($return) {
  220. return json_decode($return, true);
  221. } else {
  222. return array("error"=>"Transaction Error.");
  223. }
  224. }
  225. return array("error"=>"Webservice({$webservice}) not found.");
  226. }
  227. }