/** * (non-PHPdoc) * @see HttpClient::send() */ public function send(Request $request) { $ch = $this->curlAdapter->init($request->getUri()); $this->curlAdapter->setOption($ch, $this->curlAdapter->getOptConstant('CUSTOMREQUEST'), $request->getMethod()); $headers = array(); foreach ($request->getHeader() as $name => $value) { $headers[] = $request->getHeader()->joinField($name, $value); } $this->curlAdapter->setOption($ch, $this->curlAdapter->getOptConstant('HTTPHEADER'), $headers); $this->curlAdapter->setOption($ch, $this->curlAdapter->getOptConstant('POSTFIELDS'), $request->getPayload()); $this->curlAdapter->setOption($ch, $this->curlAdapter->getOptConstant('RETURNTRANSFER'), 1); $this->curlAdapter->setOption($ch, $this->curlAdapter->getOptConstant('HEADERFUNCTION'), array($this, 'headerCallback')); $this->response->flush(); $content = $this->curlAdapter->exec($ch); if (false === $content) { throw new \RuntimeException($this->curlAdapter->getError($ch), $this->curlAdapter->getErrno($ch)); } $this->response->setContent($content); $this->response->setStatusCode($this->curlAdapter->getInfo($ch, $this->curlAdapter->getInfoConstant('HTTP_CODE'))); $this->curlAdapter->close($ch); return clone $this->response; }
/** * Confirms that it is possible to set arbitrary HTTP verbs. */ public function testVerbs() { $request = new Request('http://127.0.0.1'); $this->assertEquals('GET', $request->getVerb()); $request->setVerb('DELETE'); $this->assertEquals('DELETE', $request->getVerb()); /* Setting a payload implicitly sets the HTTP verb to 'POST' unless another one has been set. */ $request = new Request('http://127.0.0.1'); $args = array('foo' => 'bar'); $request->setPayload($args); $this->assertEquals('POST', $request->getVerb()); $request->setVerb('PATCH'); $this->assertEquals('PATCH', $request->getVerb()); $this->assertEquals($args, $request->getPayload()); // We can set the verb and the payload in the opposite order too $request = new Request('http://127.0.0.1'); $request->setVerb('BUTT'); $payload = json_encode($args); $request->setPayload($payload); $this->assertEquals('BUTT', $request->getVerb()); $this->assertEquals($payload, $request->getPayload()); /* But if we try to force the verb to GET, we'll have a problem when we try to get the cURL options, which is the point when conflicts are resolved. */ $request->setVerb('GET'); $this->assertThrows('LogicException', array($request, 'getCurlOptions')); }
public function send(Request $request) { $this->setOptions($request->getOptions()); switch ($this->method) { case CURL::DELETE: $this->addOption(CURLOPT_CUSTOMREQUEST, CURL::DELETE); break; case CURL::PUT: $this->addOption(CURLOPT_PUT, true); $this->addOption(CURLOPT_POST, $request->getPayload()); break; case CURL::POST: /** * curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); * curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); * curl_setopt($ch, CURLOPT_HTTPHEADER, array( * 'Content-Type: application/json', * 'Content-Length: ' . strlen($data_string)) * ); */ $this->addOption(CURLOPT_CUSTOMREQUEST, CURL::POST); $this->addOption(CURLOPT_POSTFIELDS, $request->getPayload()); $this->addOption(CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'Content-Length: ' . strlen($request->getPayload()))); break; } curl_setopt_array($this->handler, $this->getOptions()); $response = Response::newResponse(curl_exec($this->handler), curl_getinfo($this->handler), curl_error($this->handler)); return $response; }