/** * Send this HTTP request * * @throws Horde_Http_Exception * @return Horde_Http_Response_Base */ public function send() { // at this time only the curl driver is supported $client = new \http\Client('curl'); $body = new \http\Message\Body(); $data = $this->data; if (is_array($data)) { $body->addForm($data); } else { $body->append($data); } $httpRequest = new \http\Client\Request($this->method, (string) $this->uri, $this->headers, $body); $client->setOptions($this->_httpOptions()); $client->enqueue($httpRequest); try { $client->send(); $httpResponse = $client->getResponse($httpRequest); } catch (\http\Exception $e) { throw new Horde_Http_Exception($e); } return new Horde_Http_Response_Peclhttp2((string) $this->uri, $httpResponse); }
/** * Processes a PUT request. * * @param string $path File path * @param string $data Data * @param boolean $isFile Determines if $data is a file name or actual data * @throws \Jyxo\Webdav\DirectoryNotCreatedException If the target directory cannot be created * @throws \Jyxo\Webdav\FileNotCreatedException If the file cannot be created * @throws \Jyxo\Webdav\Exception On error */ protected function processPut($path, $data, $isFile) { $requests = $this->createAllRequests($path, self::METHOD_PUT); foreach ($requests as $request) { if ($isFile) { $body = new \http\Message\Body(fopen($data, 'r')); $request->setBody($body); } else { $body = new \http\Message\Body(); $body->append($data); $request->setBody($body); } } $success = true; foreach ($this->sendAllRequests($requests) as $response) { switch ($response->getResponseCode()) { // Saved case self::STATUS_200_OK: case self::STATUS_201_CREATED: break; // An existing file was modified // An existing file was modified case self::STATUS_204_NO_CONTENT: break; // The directory might not exist // The directory might not exist case self::STATUS_403_FORBIDDEN: case self::STATUS_404_NOT_FOUND: case self::STATUS_409_CONFLICT: $success = false; break; // Could not save // Could not save default: throw new \Jyxo\Webdav\FileNotCreatedException(sprintf('File %s cannot be created.', $path)); } } // Saved if ($success) { return; } // Not saved, try creating the directory first if ($this->createDirectoriesAutomatically) { try { $this->mkdir(dirname($path)); } catch (DirectoryNotCreatedException $e) { throw new \Jyxo\Webdav\FileNotCreatedException(sprintf('File %s cannot be created.', $path), 0, $e); } } // Try again foreach ($this->sendAllRequests($requests) as $response) { // 201 means saved if (self::STATUS_201_CREATED !== $response->getResponseCode()) { throw new \Jyxo\Webdav\FileNotCreatedException(sprintf('File %s cannot be created.', $path)); } } }
<?php $client = new http\Client(); $request = new http\Client\Request(); $body = new http\Message\Body(); $body->append('{"number":1,"string":"f\\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}]}'); $request->setRequestUrl('http://mockbin.com/har'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setHeaders(array('content-type' => 'application/json')); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
<?php $client = new http\Client(); $request = new http\Client\Request(); $body = new http\Message\Body(); $body->append('Hello World'); $request->setRequestUrl('http://mockbin.com/har'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setHeaders(array('content-type' => 'text/plain')); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
<?php $client = new http\Client(); $request = new http\Client\Request(); $body = new http\Message\Body(); $body->append(new http\QueryString(array('foo' => 'bar'))); $request->setRequestUrl('http://mockbin.com/har'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setQuery(new http\QueryString(array('foo' => array('bar', 'baz'), 'baz' => 'abc', 'key' => 'value'))); $request->setHeaders(array('content-type' => 'application/x-www-form-urlencoded', 'accept' => 'application/json')); $client->setCookies(array('bar' => 'baz', 'foo' => 'bar')); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
<?php $client = new http\Client(); $request = new http\Client\Request(); $body = new http\Message\Body(); $body->append(new http\QueryString(array('foo' => 'bar', 'hello' => 'world'))); $request->setRequestUrl('http://mockbin.com/har'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setHeaders(array('content-type' => 'application/x-www-form-urlencoded')); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();