/** * @param Request $request * @return mixed */ function send(Request $request) { if (($method = $request->getMethod()) === $request::METHOD_GET) { $url = $request->getUrl(); $request->setUrl($url . (strpos($url, '?') ? '&' : '?') . http_build_query($request->getParameters(), '', '&')); } $resource = curl_init($request->getUrl()); curl_setopt($resource, CURLINFO_HEADER_OUT, TRUE); curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE); $headers = []; foreach ($request->getHeaders() as $key => $value) { $headers[] = "{$key}: {$value} "; } curl_setopt($resource, CURLOPT_HTTPHEADER, $headers); if ($method === $request::METHOD_GET) { curl_setopt($resource, CURLOPT_HTTPGET, TRUE); } else { if ($method === $request::METHOD_POST) { curl_setopt($resource, CURLOPT_POST, TRUE); } } $return = curl_exec($resource); curl_close($resource); return $return; }
protected function createContext(Request $request) { $context = $this->context; $context['http']['method'] = $request->getMethod(); $headers = $request->getHeaders(); foreach ($headers as $key => $value) { $context['http']['header'][] = "{$key}: {$value}"; } if (count($request->getParameters())) { if ($request->getMethod() !== Request::METHOD_GET) { $context['http']['header'][] = 'Content-type: application/x-www-form-urlencoded'; $context['http']['context'] = http_build_query($request->getParameters(), '', '&'); } else { $url = $request->getUrl(); $request->setUrl($url . (strpos($url, '?') ? '&' : '?') . http_build_query($request->getParameters(), '', '&')); } } return stream_context_create($context); }