public function testGetUriWithHandlerAndParams() { $params = array('param1' => 1, 'param2' => array(2, 3)); $this->request->setHandler('myHandler'); $this->request->addParams($params); $this->assertEquals('myHandler?param1=1¶m2=2¶m2=3', $this->request->getUri()); }
/** * * adapt Request to HttpRequest * * {@link http://us.php.net/manual/en/http.constants.php * HTTP Predefined Constant} * * {@link http://us.php.net/manual/en/http.request.options.php * HttpRequest options} * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @param HttpRequest * @return \HttpRequest */ public function toHttpRequest($request, $endpoint) { $url = $endpoint->getBaseUri() . $request->getUri(); $httpRequest = new \HttpRequest($url); $headers = array(); foreach ($request->getHeaders() as $headerLine) { list($header, $value) = explode(':', $headerLine); if ($header = trim($header)) { $headers[$header] = trim($value); } } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { $headers['Authorization'] = 'Basic ' . base64_encode($authData['username'] . ':' . $authData['password']); } switch ($request->getMethod()) { case Request::METHOD_GET: $method = HTTP_METH_GET; break; case Request::METHOD_POST: $method = HTTP_METH_POST; if ($request->getFileUpload()) { $httpRequest->addPostFile('content', $request->getFileUpload(), 'application/octet-stream; charset=binary'); } else { $httpRequest->setBody($request->getRawData()); if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'text/xml; charset=utf-8'; } } break; case Request::METHOD_HEAD: $method = HTTP_METH_HEAD; break; default: throw new InvalidArgumentException('Unsupported method: ' . $request->getMethod()); } $httpRequest->setMethod($method); $httpRequest->setOptions(array('timeout' => $endpoint->getTimeout(), 'connecttimeout' => $endpoint->getTimeout(), 'dns_cache_timeout' => $endpoint->getTimeout())); $httpRequest->setHeaders($headers); return $httpRequest; }
/** * Create curl handle for a request * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @return resource */ public function createHandle($request, $endpoint) { // @codeCoverageIgnoreStart $uri = $endpoint->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->createOptions($request, $endpoint); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $uri); curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); if (!ini_get('open_basedir')) { curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); } curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']); curl_setopt($handler, CURLOPT_CONNECTTIMEOUT, $options['timeout']); if ($proxy = $this->getOption('proxy')) { curl_setopt($handler, CURLOPT_PROXY, $proxy); } if (!isset($options['headers']['Content-Type'])) { if ($method == Request::METHOD_GET) { $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; } else { $options['headers']['Content-Type'] = 'application/xml; charset=utf-8'; } } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { curl_setopt($handler, CURLOPT_USERPWD, $authData['username'] . ':' . $authData['password']); curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if (count($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $value) { $headers[] = $key . ": " . $value; } curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); } if ($method == Request::METHOD_POST) { curl_setopt($handler, CURLOPT_POST, true); if ($request->getFileUpload()) { if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $curlFile = curl_file_create($request->getFileUpload()); curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => $curlFile)); } else { curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => '@' . $request->getFileUpload())); } } else { curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); } } elseif ($method == Request::METHOD_GET) { curl_setopt($handler, CURLOPT_HTTPGET, true); } elseif ($method == Request::METHOD_HEAD) { curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); } else { throw new InvalidArgumentException("unsupported method: {$method}"); } return $handler; // @codeCoverageIgnoreEnd }
/** * Create curl handle for a request * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @return resource */ public function createHandle($request, $endpoint) { // @codeCoverageIgnoreStart $uri = $endpoint->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->createOptions($request, $endpoint); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $uri); curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']); /* * Begin Added for Pantheon * Subclass and jsu override createHandle */ curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false); if ($endpoint->getOption('ssl')['local_cert']) { curl_setopt($handler, CURLOPT_SSLCERT, realpath($endpoint->getOption('ssl')['local_cert'])); } if ($endpoint->getPort()) { curl_setopt($handler, CURLOPT_PORT, $endpoint->getPort()); } // End Added for pantheon if ($proxy = $this->getOption('proxy')) { curl_setopt($handler, CURLOPT_PROXY, $proxy); } if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { curl_setopt($handler, CURLOPT_USERPWD, $authData['username'] . ':' . $authData['password']); curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if (count($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $value) { $headers[] = $key . ": " . $value; } curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); } if ($method == Request::METHOD_POST) { curl_setopt($handler, CURLOPT_POST, true); if ($request->getFileUpload()) { curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => '@' . $request->getFileUpload())); } else { curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); } } elseif ($method == Request::METHOD_GET) { curl_setopt($handler, CURLOPT_HTTPGET, true); } elseif ($method == Request::METHOD_HEAD) { curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); } else { throw new InvalidArgumentException("unsupported method: {$method}"); } return $handler; // @codeCoverageIgnoreEnd }