12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 11/23/15
- * Time: 10:56 PM
- */
-
- namespace Luticate\Utils;
-
-
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\ClientException;
- use GuzzleHttp\Exception\RequestException;
-
- class LuRequest
- {
- /**
- * @param $method
- * @param $url
- * @param array $get
- * @param array $post
- * @param array $headers
- * @return LuRequestDbo
- */
- public static function request($method, $url, $get = [], $post = null, $headers = [])
- {
- $client = new Client();
- $options = [
- "headers" => $headers,
- "form_params" => $post
- ];
- foreach ($get as $key => $value) {
- $url .= (strpos($url, '?') === false ? "?" : "&") . urlencode($key) . "=" . urlencode($value);
- }
- $response = null;
- try {
- $response = $client->request($method, $url, $options);
- } catch (ClientException $e) {
- $response = $e->getResponse();
- LuLog::log($response->getBody());
- LuLog::log($e);
- } catch (RequestException $e) {
- $response = $e->getResponse();
- LuLog::log($response->getBody());
- LuLog::log($e);
- } catch (\Exception $e) {
- LuLog::log($e);
- }
- if (!is_null($response)) {
- $body = json_decode($response->getBody(), true);
- $request = LuRequestDbo::jsonDeserialize($body);
- $request->setStatusCode($response->getStatusCode());
- return $request;
- }
- $request = new LuRequestDbo();
- $request->setMessage("Failed to parse response");
- $request->setStatusCode(500);
- return $request;
- }
-
- public static function proxy($method, $url, $get = [], $post = null, $headers = [])
- {
- $response = LuRequest::request($method, $url, $get, $post, $headers);
- if ($response->getStatusCode() != 200) {
- abort($response->getStatusCode(), $response->getMessage());
- }
- return $response->getData();
- }
- }
|