private function send($requestType, $url, $data = null, $extraHeaders = []) { $extraHeaders = array_merge($this->extraHeaders, $extraHeaders); $request = new \Curl\Request($this->baseUrl . $url); $request->timeout = $this->timeout; $request->headers = $extraHeaders; $request->requestType = strtoupper($requestType); if ($data) { $request->headers['Content-Type'] = 'application/json'; $request->data = json_encode($data); } $response = $request->exec(); if ($response->contentType == 'application/json') { return json_decode($response->data); } else { return $response->data; } }
/** * test send a copy of request */ public function testCloneRequest() { $request = Curl\Request::newRequest($this->getTestCallbackUrl()); $params = array('param1' => 1, 'param2' => 2); $request->setMethod('POST')->addPostParams($params); $result = json_decode($request->send(), true); $this->assertInternalType('array', $result); $this->assertArrayHasKey('param2', $result['params']); $result = null; try { $result = $request->send(); } catch (\BadMethodCallException $e) { } $this->assertNull($result); $request2 = clone $request; $result = $result = json_decode($request2->send(), true); $this->assertInternalType('array', $result); $this->assertArrayHasKey('param2', $result['params']); }
<?php $request = new Curl\Request(); $request->setUrl('http://google.com'); // or $request = Curl\Request::newRequest('http://google.com'); // making POST request $request = Curl\Request::newRequest('http://example.com', 'POST', array('param1' => 1, 'param2' => 2)); // add attachments $request->attachFiles(array('file' => 'filename.txt', 'file2' => array('filename1.txt', 'filename2.txt'))); $response = $request->getResponse(); // or $response = $request->send(); echo $response->getContent(); // or echo $response;
<?php $request = new Curl\Request('http://google.com'); $request->setTimeout(30)->setConnectionTimeout(5)->setAllowRedirect(true, 5)->setRefererUrl('http://some.url'); $request->setHeaders(array('Accept: text/html', 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0'))->addHeaders(array('Cache-Control: max-age=0')); $request->setOptions(array(CURLOPT_VERBOSE => false, CURLOPT_FAILONERROR => true))->addOptions(array(CURLINFO_HEADER_OUT => true)); $request->on('beforeSend', function ($request, Curl\Request $request) { // before send the request })->on('success', function (Curl\Response\PlainResponse $response, Curl\Request $request) { // if response has no error // $response->hasError() retuned false })->on('error', function (Curl\Response\PlainResponse $response, Curl\Request $request) { // if response has an error // $response->hasError() retuned true })->on('compete', function (Curl\Response\PlainResponse $response, Curl\Request $request) { // always on complete request }); $response = $request->send(); // retrieve request info $info = $response->getInfo(); var_dump($response->getInfo(true)->http_code, $info['content_type']); /** * Custom Response class */ class CustomResponse extends Curl\Response\PlainResponse { /** * (non-PHPdoc) * @see Curl.Response::hasError() */ public function hasError()