Webservice.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. * @param array $data
  77. *
  78. * @return HttpResponse
  79. */
  80. public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  81. {
  82. $request = new HttpRequest($method, $url);
  83. $headers = array();
  84. if (!empty($data)) {
  85. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  86. $request->setContent(http_build_query($data));
  87. }
  88. $request->setHeaders($headers);
  89. $response = new HttpResponse();
  90. $this->httpClient->send($request, $response);
  91. return $response->getContent();
  92. }
  93. /**
  94. * @param string $url
  95. * @param string $method
  96. *
  97. * @return HttpResponse
  98. */
  99. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
  100. {
  101. $response = '';
  102. if ($token = $this->securityTokenStorage->getToken()) {
  103. $headers = array();
  104. $request = new HttpRequest($method, $url);
  105. if (!empty($data)) {
  106. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  107. $request->setContent(http_build_query($data));
  108. }
  109. $response = new HttpResponse();
  110. if (method_exists($token, 'getAccessToken')) {
  111. $headers[] = 'Authorization: Bearer ' . $token->getAccessToken();
  112. }
  113. $request->setHeaders($headers);
  114. $this->httpClient->send($request, $response);
  115. $response = $response->getContent();
  116. }
  117. return $response;
  118. }
  119. /**
  120. * @param string $url
  121. * @param array $filters
  122. * @param array $order_by
  123. * @param integer $limit
  124. * @param integer $offset
  125. *
  126. * @return array
  127. */
  128. public function getData($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  129. {
  130. $data = array();
  131. try {
  132. $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
  133. $data = json_decode($this->makeGetRequest($url), true);
  134. } catch (\Exception $ex) {}
  135. return $data;
  136. }
  137. /**
  138. * @param string $url
  139. * @param array $filters
  140. * @param array $order_by
  141. * @param integer $limit
  142. * @param integer $offset
  143. *
  144. * @return array
  145. */
  146. public function get($url, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  147. {
  148. $data = array();
  149. try {
  150. $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
  151. $data = json_decode($this->makeRequest($url), true);
  152. } catch (\Exception $ex) {}
  153. return $data;
  154. }
  155. /**
  156. * @param string $webservice
  157. * @param array $filters
  158. * @param array $order_by
  159. * @param int $limit
  160. * @param int $offset
  161. *
  162. * @return string
  163. */
  164. public function buildUrl($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  165. {
  166. $url = $webservice . '?';
  167. if ($this->serviceContainer->hasParameter($webservice)) {
  168. $url = $this->serviceContainer->getParameter($webservice) . '?';
  169. }
  170. if ($filters) {
  171. $url .= http_build_query(array('filters' => $filters));
  172. }
  173. if ($order_by) {
  174. $url .= '&' . http_build_query(array('order_by' => $order_by));
  175. }
  176. if ($limit) {
  177. $url .= "&limit={$limit}";
  178. }
  179. if ($offset) {
  180. $url .= "&offset={$offset}";
  181. }
  182. return $url;
  183. }
  184. /**
  185. * @param string $webservice
  186. * @param int $id
  187. *
  188. * @return string
  189. */
  190. public function getById($webservice, $id)
  191. {
  192. $result = $this->getArray($webservice, array(
  193. 'id' => $id
  194. ));
  195. return isset($result[0]) && isset($result[0]['id']) && isset($result[0]['name'])
  196. ? "{$result[0]['id']} - {$result[0]['name']}"
  197. : $id;
  198. }
  199. /**
  200. * @param string $webservice
  201. * @param int $id
  202. * @param array $data
  203. *
  204. * @return array
  205. */
  206. public function putData($webservice, $id, $data)
  207. {
  208. if ($this->serviceContainer->hasParameter($webservice)) {
  209. $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  210. $data_string = json_encode($data);
  211. $ch = curl_init($url);
  212. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  213. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  214. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($data_string)));
  215. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  216. try {
  217. $return = curl_exec($ch);
  218. } catch (\Exception $ex) {
  219. return array("error"=>"Connection Error.");
  220. }
  221. if($return) {
  222. return json_decode($return, true);
  223. } else {
  224. return array("error"=>"Transaction Error.");
  225. }
  226. }
  227. return array("error"=>"Webservice({$webservice}) not found.");
  228. }
  229. }