1: <?php
2: namespace Genetsis\core;
3:
4: use \Exception;
5: use Genetsis\Config;
6: use Genetsis\Identity;
7: use Genetsis\Logger;
8:
9:
10: class Request
11: {
12:
13: const HTTP_POST = 'POST';
14: const HTTP_PUT = 'PUT';
15: const HTTP_GET = 'GET';
16: const HTTP_DELETE = 'DELETE';
17: const HTTP_HEAD = 'HEAD';
18:
19: const SECURED = true;
20: const NOT_SECURED = false;
21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: public static function execute($url, $parameters = array(), $http_method = self::HTTP_GET, $credentials = self::NOT_SECURED, $http_headers = array(), $cookies = array())
40: {
41: if (!extension_loaded('curl')) {
42: throw new Exception('The PHP extension curl must be installed to use this library.');
43: }
44:
45: if (($url = trim($url)) == '') {
46: return array(
47: 'result' => false,
48: 'code' => 0,
49: 'content_type' => ''
50: );
51: }
52: $is_ssl = (preg_match('#^https#Usi', $url)) ? true : false;
53:
54: $curl_options = array(
55: CURLOPT_RETURNTRANSFER => true,
56: CURLOPT_CUSTOMREQUEST => $http_method,
57: CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT']
58: );
59:
60: if ($is_ssl) {
61: $curl_options[CURLOPT_SSL_VERIFYPEER] = false;
62: $curl_options[CURLOPT_SSL_VERIFYHOST] = 0;
63: } else {
64: $curl_options[CURLOPT_SSL_VERIFYPEER] = true;
65: }
66:
67: if ($credentials) {
68: $parameters['client_id'] = OAuthConfig::getClientId();
69: $parameters['client_secret'] = OAuthConfig::getClientSecret();
70: }
71:
72: switch ($http_method) {
73: case self::HTTP_POST:
74: $curl_options[CURLOPT_POST] = true;
75:
76: if (isset($http_headers['Content-Type'])
77: && $http_headers['Content-Type'] == 'application/json'
78: && !empty($parameters) && is_array($parameters)
79: ) {
80:
81: $curl_options[CURLOPT_POSTFIELDS] = json_encode($parameters);
82: }
83: else {
84: $curl_options[CURLOPT_POSTFIELDS] = http_build_query($parameters);
85: }
86: break;
87:
88: case self::HTTP_PUT:
89: $curl_options[CURLOPT_POSTFIELDS] = http_build_query($parameters);
90: break;
91:
92: case self::HTTP_HEAD:
93: $curl_options[CURLOPT_NOBODY] = true;
94:
95: case self::HTTP_DELETE:
96:
97: if (isset($http_headers['Content-Type'])
98: && $http_headers['Content-Type'] == 'application/json'
99: && !empty($parameters) && is_array($parameters)
100: ) {
101: $curl_options[CURLOPT_POSTFIELDS] = json_encode($parameters);
102: }
103: else {
104: $url .= '?' . http_build_query($parameters, null, '&');
105: }
106: break;
107: case self::HTTP_GET:
108: if (!empty($parameters)) {
109: $url .= '?' . http_build_query($parameters, null, '&');
110: }
111: break;
112: default:
113: break;
114: }
115:
116: $curl_options[CURLOPT_URL] = $url;
117:
118:
119: if (is_array($cookies) && !empty($cookies)) {
120:
121: foreach ($cookies as $key => $value) {
122: $cookies[$key] = rtrim($value, ';');
123: }
124: $curl_options[CURLOPT_COOKIE] = implode('; ', $cookies);
125: }
126:
127:
128: if (is_array($http_headers) && !empty($http_headers)) {
129: $header = array();
130: foreach ($http_headers as $key => $parsed_urlvalue) {
131: $header[] = "$key: $parsed_urlvalue";
132: }
133: $curl_options[CURLOPT_HTTPHEADER] = $header;
134: }
135:
136:
137: $ch = curl_init();
138: curl_setopt_array($ch, $curl_options);
139: $result = curl_exec($ch);
140: $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
141: $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
142: $total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
143: curl_close($ch);
144:
145: Identity::getLogger()->debug('### BEGIN REQUEST ###');
146: Identity::getLogger()->debug(sprintf('URL -> [%s][%s] %s', $http_method, ($is_ssl ? 'ssl' : 'no ssl'), var_export($url, true)));
147: Identity::getLogger()->debug('Params -> ' . var_export($parameters, true));
148: Identity::getLogger()->debug('Headers -> ' . var_export($http_headers, true));
149: Identity::getLogger()->debug(sprintf("Response -> [%s][%s]\n%s", $content_type, $http_code, var_export($result, true)));
150: Identity::getLogger()->debug('Total Time -> ' . var_export($total_time, true));
151: Identity::getLogger()->debug('### END REQUEST ###');
152:
153: return array(
154: 'result' => ($content_type === 'application/json') ? ((null === json_decode($result)) ? $result : json_decode($result)) : $result,
155: 'code' => $http_code,
156: 'content_type' => $content_type
157: );
158: }
159: }