/** * @example * $content = array( * 'foo' => 'bar', * 'file' => array( * 'content' => 'file content is simple text', // or path, mantatory * 'name' => 'filename.txt', // optional * 'type' => 'text/plain' // optional * )); * * @param string $url * @param array $content * @return CUrl - call ->exec() */ static function postUploadFile($url, array $content) { $eol = "\r\n"; $boundary = md5(microtime(TRUE)); $curl = new CUrl($url); $curl->setOptions(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_VERBOSE => 1, CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data; charset=utf-8; boundary="' . $boundary . '"'))); $body = ''; foreach ($content as $name => $value) { $body .= '--' . $boundary . $eol; $body .= 'Content-Disposition: form-data;name="' . $name . '"'; if (is_array($value)) { if (file_exists($value['content'])) { $type = MimeTypeDetector::fromFile($value['content']); $content = file_get_contents($value['content']); } else { $type = MimeTypeDetector::fromString($content = $value['content']); } // is file $body .= '; filename="' . (isset($value['name']) ? $value['name'] : date('YmdHis')) . '"' . $eol; $body .= 'Content-Type: ' . (isset($value['type']) ? $value['type'] : $type) . $eol; if (preg_match('~base64~i', $content)) { $body .= 'Content-Transfer-Encoding: base64' . $eol; $content = preg_replace('~^base64~i', '', $content); } $body .= $eol; // $body .= chunk_split(base64_encode($content)); // RFC 2045 $body .= trim($content) . $eol; } else { $body .= $eol . $eol . $value . $eol; } } $body .= "--{$boundary}--" . $eol . $eol; $curl->setopt(CURLOPT_POSTFIELDS, $body); return $curl; }
/** * * @param CUrl $curl * @param string $name * @return int */ public function addHandle(CUrl $curl, $name = NULL) { if ($name === NULL) { $name = $curl->getInfo('url'); } $this->handles[$name] = $curl->getResource(); return curl_multi_add_handle($this->resource, $curl->getResource()); }
public function _testCurlShare() { // je funkční ale jestli pracuje správně??? $a = new CUrl('http://example.com/'); $a->enableShare(); dump($a->exec()); $b = new CUrl('http://php.net/'); $b->enableShare(); dump($b->exec()); }
/** * @todo check filesize ??? 2MB in documentation * @param string $filename * @return XMLResponse * @throws FioException */ private function send($filename) { $curl = new CUrl(self::REST_URL_WRITE); $curl->setOptions(array(CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_VERBOSE => 0, CURLOPT_TIMEOUT => 60, CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data; charset=utf-8;'), CURLOPT_POSTFIELDS => array('type' => $this->uploadExtension, 'token' => $this->token, 'lng' => $this->language, 'file' => $curl->fileCreate($filename)))); $this->availableAnotherRequest(); $xml = trim($curl->exec()); if (!$xml) { throw new FioException('FIO server is not responding.', 500); } return $this->response = new XMLResponse($xml); }