public function upload($filepath, $public = 0) { $filename = realpath($filepath); $url = 'http://storage.byte.gs/file'; $finfo = new \finfo(FILEINFO_MIME_TYPE); $mimetype = $finfo->file($filename); $ch = curl_init($url); $request_headers = ["X-Auth-Token: " . $this->apiKey]; $cfile = curl_file_create($filename, $mimetype, basename($filename)); $data = ['file' => $cfile, 'public' => $public]; curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $r = curl_getinfo($ch); if ($r["http_code"] != 200) { $detais = json_decode($result, true); if (isset($detais["msg"])) { throw new \Exception($detais["msg"], 1); } else { throw new \Exception("HTTP Return " . $r["http_code"], 1); } } $detais = json_decode($result, true); $res = array(); $res["id"] = $detais["id"]; $res["md5"] = $detais["md5"]; if ($public == 1) { $res["link"] = "http://storage.byte.gs/file/" . $res["id"] . "?md5=" . $res["md5"]; } return $res; }
protected static function getPostField($path, $mimeType, $name) { if (function_exists('curl_file_create')) { return curl_file_create($path, $mimeType, $name); } return '@' . $path; }
public function testProceduralCurlFileWithMimeAndPostname() { $file = curl_file_create($this->testFilename, 'foo', 'bar'); curl_setopt($this->handle, CURLOPT_POSTFIELDS, array('file' => $file)); $output = curl_exec($this->handle); $this->assertEquals('bar|foo|6', $output); }
/** * 將 $fp 上傳到 SheetHub 取得 $upload_id 待下一步 * * @param mixed $fp * @access public * @return string $upload_id */ public function uploadToSheetHub($fp, $file_type) { $sheethub_domain = getenv('SHEETHUB_DOMAIN') ?: 'sheethub.com'; $sheethub_key = getenv('SHEETHUB_KEY'); if (is_string($fp)) { $file = $fp; } else { $file = stream_get_meta_data($fp)['uri']; } $map = array('csv' => array('text/csv', 'data.csv'), 'zip' => array('text/zip', 'data.zip'), 'rar' => array('text/rar', 'data.rar'), 'xml' => array('text/xml', 'data.xml'), 'json' => array('text/json', 'data.json'), 'xls' => array('text/xls', 'data.xls'), 'xlsx' => array('text/xlsx', 'data.xlsx')); if (!array_key_exists($file_type, $map)) { throw new Exception("unknown filetype {$file_type}"); } $curl = curl_init("https://{$sheethub_domain}/file/upload?access_token={$sheethub_key}"); $cfile = curl_file_create($file, $map[$file_type][0], $map[$file_type][1]); curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => $cfile)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $content = curl_exec($curl); curl_close($curl); if (!($upload_id = json_decode($content)->data->upload_id)) { throw new Exception('upload failed:' . $content); } error_log("upload to sheethub done: upload_id = {$upload_id}"); return $upload_id; }
/** * 智能鉴黄-Files * @param string $pornFile 要进行黄图检测的图片File列表 */ public static function pornDetectFile($pornFile) { $sign = Auth::getPornDetectSign(); if (false === $sign) { $data = array("code" => 9, "message" => "Secret id or key is empty.", "data" => array()); return $data; } $data = array('appid' => Conf::APPID, 'bucket' => Conf::BUCKET); for ($i = 0; $i < count($pornFile); $i++) { if (PATH_SEPARATOR == ';') { // WIN OS $pornFile[$i] = iconv("UTF-8", "gb2312", $pornFile[$i]); } $srcPath = realpath($pornFile[$i]); if (!file_exists($srcPath)) { return array('httpcode' => 0, 'code' => self::IMAGE_FILE_NOT_EXISTS, 'message' => 'file ' . $pornFile[$i] . ' not exists', 'data' => array()); } if (function_exists('curl_file_create')) { $data['image[' . (string) $i . ']'] = curl_file_create($srcPath, NULL, $pornFile[$i]); } else { $data['image[' . (string) $i . ']'] = '@' . $srcPath; } } $req = array('url' => Conf::API_PRONDETECT_URL, 'method' => 'post', 'timeout' => self::TIME_OUT, 'data' => $data, 'header' => array('Authorization:' . $sign)); $rsp = Http::send($req); return $rsp; }
function uploadSkin($inputFile) { $skinFile = curl_file_create($inputFile, 'image/png', 'skin.png'); $post = array('authenticityToken' => AUTH_TOKEN, 'model' => 'steve', 'skin' => $skinFile); $ch = curl_init(); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_URL, 'https://minecraft.net/profile/skin'); curl_setopt($ch, CURLOPT_COOKIE, USER_COOKIE); curl_setopt($ch, CURLOPT_REFERER, "https://minecraft.net/profile"); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: minecraft.net', 'Origin: https://minecraft.net', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (DEBUG) { $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT); echo "\n.\n"; echo $headerSent; echo "\n.\n"; echo $output; echo "\n.\n"; } if ($httpcode != 302) { echo "Failed to upload skin\n"; if (!DEBUG) { echo $output; } return false; } curl_close($ch); return true; }
public function create($data) { curl_file_create('./'); if (!$this->curl) { return array(); } if (empty($data)) { return false; } $this->curl->setSubmitType('post'); $this->curl->setReturnFormat('json'); $this->curl->initPostData(); if (is_array($data) && count($data) > 0) { foreach ($data as $k => $v) { if (is_array($v)) { $this->array_to_add($k, $v); } else { $this->curl->addRequestData($k, $v); } } } $this->curl->addRequestData('a', 'create_tuji'); $ret = $this->curl->request('admin/tuji_update.php'); return $ret[0]; }
public function addFile($field_name, $absolute_filename_path) { $file_info = new \finfo(FILEINFO_MIME); $mime_type = $file_info->buffer(file_get_contents($absolute_filename_path)); $mime = explode(';', $mime_type); $this->files[$field_name] = curl_file_create($absolute_filename_path, reset($mime), basename($absolute_filename_path)); }
public static function upFile($local, $remote) { switch (\Config::get("services.fileuse")) { case "cdn": $data = ['file' => curl_file_create($local), 'path' => $remote, 'md5' => md5_file($local)]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, \Config::get('services.fileurl.cdn.api')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); if ($output == 'SUCCESS') { return $remote; } else { return false; } break; default: $save = public_path() . "/uploads" . $remote; $path = dirname($save); if (!is_dir($path)) { mkdir($path, 0777, true); } if (copy($local, $save)) { unlink($local); return $remote; } else { return false; } break; } }
/** * @param string $command name of the command * @param array $data data of the command * * @return array with the data of the reply */ function odt_collabeditor_call_typist2($command, $data, $filepath, $filetype, $filetitle) { $fullcommand = array("command" => $command, "data" => (object) $data); $fullcommand_string = json_encode($fullcommand); $cfile = curl_file_create($filepath, $filetype, $filetitle); $postdata = array('message' => $fullcommand_string, 'document' => $cfile); $typist_control_host = elgg_get_plugin_setting('typist_control_host', 'odt_collabeditor'); $ch = curl_init($typist_control_host . '/COMMAND2'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); $output = curl_exec($ch); if ($output === false) { $output = array("success" => false, "error" => "EBADREPLY", "errorString" => curl_error($ch)); } else { $output = json_decode($output); // TODO: decoding error handling if (!array_key_exists("success", $output) || $output->success && !array_key_exists("data", $output) || !$output->success && !array_key_exists("error", $output)) { $output = array("success" => false, "error" => "EBADREPLY", "errorString" => "Bad reply data."); } } // ensure dummy error string if (array_key_exists("error", $output) && !array_key_exists("errorString", $output)) { $output->errorString = "Unknown error."; } error_log('Result of typist call: ' . json_encode($output)); curl_close($ch); return $output; }
function UploadToBaiDu($filedata, $filetype, $filename) { //把保存的文件放到upload目录,因为上传还是要认文件名的 $serverPath = "upload/" . $_FILES["file"]["name"]; move_uploaded_file($filedata, $serverPath); //定义一下上传的路径 define('BASE_PATH', str_replace('\\', '/', realpath(dirname(__FILE__) . '/')) . "/"); //文件的全路径 $serverFullPath = BASE_PATH . $serverPath; $data = array('compresstime' => '', 'filetype' => '', 'newfilesize' => '', 'Filename' => $filename, 'filewidth' => '', 'filesize' => '', 'fileheight' => '', 'Upload' => 'Submit Query', 'filedata' => curl_file_create($serverFullPath, $filetype, $filename)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://image.baidu.com/pictureup/uploadshitu?fr=flash&fm=index&pos=upload'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36', 'Referer: http://img.baidu.com/img/image/stu/STUpload2.swf?v=0108', 'X-Forwarded-For: 8.8.8.8', 'Forwarded-For: 8.8.8.8', 'Origin: http://img.baidu.com', 'X-Requested-With: ShockwaveFlash/15.0.0.239')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch); curl_close($ch); preg_match('/queryImageUrl=(.+?)&/', $html, $ret); //var_dump($ret); if (count($ret) === 2) { unlink($serverFullPath); return urldecode($ret[1]); } return FALSE; }
public function startJob($sourceFilePath, $targetFormat = 'txt') { // Since PHP 5.5+ CURLFile is the preferred method for uploading files if (function_exists('curl_file_create')) { $sourceFile = curl_file_create($sourceFilePath); } else { $sourceFile = '@' . realpath($sourceFilePath); } $postData = array("xlsfile" => $sourceFile, "target_format" => $targetFormat); $ch = curl_init(); // Init curl curl_setopt($ch, CURLOPT_URL, $this->endpoint . '/jobs'); // API endpoint curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // Enable the @ prefix for uploading files curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey . ":"); // Set the API key as the basic auth username $body = curl_exec($ch); if (curl_errno($ch)) { print_r(curl_error($ch)); } curl_close($ch); $this->job = json_decode($body, true); echo "Response:\n---------\n"; print_r($this->job); }
function upload_crash_report($files, $agent) { global $g; $post = array(); $counter = 0; foreach ($files as $filename) { if (is_link($filename) || $filename == '/var/crash/minfree.gz' || $filename == '/var/crash/bounds.gz') { continue; } $post["file{$counter}"] = curl_file_create($filename, "application/x-gzip", basename($filename)); $counter++; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://crash.opnsense.org/'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_VERBOSE, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data;')); $response = curl_exec($ch); curl_close($ch); return !$response; }
function SendLabel($target, $file) { //$target = "http://192.168.0.205:8080/labelupload/"; # http://php.net/manual/en/curlfile.construct.php // Create a CURLFile object / procedural method //$cfile = curl_file_create('resource/test.png','image/png','testpic'); // try adding $cfile = curl_file_create($file); // Create a CURLFile object / oop method #$cfile = new CURLFile('resource/test.png','image/png','testpic'); // uncomment and use if the upper procedural method is not working. // Assign POST data $imgdata = array('upl' => $cfile); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $target); curl_setopt($curl, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15', 'Referer: http://someaddress.tld', 'Content-Type: multipart/form-data')); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); // enable posting curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // post images curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload $r = curl_exec($curl); curl_close($curl); }
/** * @param $file * @param $postName * @param string $contentType * @return \CURLFile|string */ protected static function getFileBody($file, $postName, $contentType = 'image/jpeg') { if (!file_exists($file)) { return ''; } return curl_file_create($file, $contentType, $postName); }
/** * Prepares a file for upload. To be used inside the parameters declaration for a request. * @param string $filename The file path * @param string $mimetype MIME type * @param string $postname the file name * @return string|\CURLFile */ public static function add($filename, $mimetype = '', $postname = '') { if (function_exists('curl_file_create')) { return curl_file_create($filename, $mimetype = '', $postname = ''); } else { return sprintf('@%s;filename=%s;type=%s', $filename, $postname ?: basename($filename), $mimetype); } }
function _smsc_read_url($url, $files) { $ret = ""; $post = BuyCore::$buysmscoptions['methodpost'] || strlen($url) > 2000 || $files; if (function_exists("curl_init")) { static $c = 0; // keepalive if (!$c) { $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($c, CURLOPT_TIMEOUT, 60); curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); } curl_setopt($c, CURLOPT_POST, $post); if ($post) { list($url, $post) = explode("?", $url, 2); if ($files) { parse_str($post, $m); foreach ($m as $k => $v) { $m[$k] = isset($v[0]) && $v[0] == "@" ? sprintf("%s", $v) : $v; } $post = $m; foreach ($files as $i => $path) { if (file_exists($path)) { $post["file" . $i] = function_exists("curl_file_create") ? curl_file_create($path) : "@" . $path; } } } curl_setopt($c, CURLOPT_POSTFIELDS, $post); } curl_setopt($c, CURLOPT_URL, $url); $ret = curl_exec($c); } elseif ($files) { if (BuyCore::$buysmscoptions['debug']) { echo "Не установлен модуль curl для передачи файлов\n"; } } else { if (!BuyCore::$buysmscoptions['https'] && function_exists("fsockopen")) { $m = parse_url($url); if (!($fp = fsockopen($m["host"], 80, $errno, $errstr, 10))) { $fp = fsockopen("212.24.33.196", 80, $errno, $errstr, 10); } if ($fp) { fwrite($fp, ($post ? "POST {$m['path']}" : "GET {$m['path']}?{$m['query']}") . " HTTP/1.1\r\nHost: smsc.ru\r\nUser-Agent: PHP" . ($post ? "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($m['query']) : "") . "\r\nConnection: Close\r\n\r\n" . ($post ? $m['query'] : "")); while (!feof($fp)) { $ret .= fgets($fp, 1024); } list(, $ret) = explode("\r\n\r\n", $ret, 2); fclose($fp); } } else { $ret = file_get_contents($url); } } return $ret; }
/** * Send a document to SignRequest. * @param string $file The absolute path to a file. * @param string $identifier * @param string $callbackUrl * @return CreateDocumentResponse * @throws Exceptions\SendSignRequestException */ public function createDocument($file, $identifier, $callbackUrl = null) { $file = curl_file_create($file); $response = $this->newRequest("documents")->setHeader("Content-Type", "multipart/form-data")->setData(['file' => $file, 'external_id' => $identifier, 'events_callback_url' => $callbackUrl])->send(); if ($this->hasErrors($response)) { throw new Exceptions\SendSignRequestException($response); } return new CreateDocumentResponse($response); }
function nxs_uplImgtoVK($imgURL, $options) { $postUrl = 'https://api.vkontakte.ru/method/photos.getWallUploadServer?gid=' . str_replace('-', '', $options['pgIntID']) . '&access_token=' . $options['vkAppAuthToken']; $response = wp_remote_get($postUrl); $thumbUploadUrl = $response['body']; if (!empty($thumbUploadUrl)) { $thumbUploadUrlObj = json_decode($thumbUploadUrl); $VKuploadUrl = $thumbUploadUrlObj->response->upload_url; } // prr($thumbUploadUrlObj); echo "UURL=====-----"; if (!empty($VKuploadUrl)) { // if (stripos($VKuploadUrl, '//pu.vkontakte.ru/c')!==false) { $c = 'c'.CutFromTo($VKuploadUrl, '.ru/c', '/'); $VKuploadUrl = str_ireplace('/pu.','/'.$c.'.',str_ireplace($c.'/','',$VKuploadUrl)); } $remImgURL = urldecode($imgURL); $urlParced = pathinfo($remImgURL); $remImgURLFilename = $urlParced['basename']; $imgData = wp_remote_get($remImgURL); $imgData = $imgData['body']; $tmp = array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()] = tmpfile()))); if (!is_writable($tmp)) { return "Your temporary folder or file (file - " . $tmp . ") is not witable. Can't upload image to VK"; } rename($tmp, $tmp .= '.png'); register_shutdown_function(create_function('', "unlink('{$tmp}');")); file_put_contents($tmp, $imgData); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $VKuploadUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck === true) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } if (function_exists('curl_file_create')) { $file = curl_file_create($tmp); curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => $file)); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => '@' . $tmp)); } $response = curl_exec($ch); $errmsg = curl_error($ch); curl_close($ch); //prr($response); $uploadResultObj = json_decode($response); // prr($response); //prr($uploadResultObj); if (!empty($uploadResultObj->server) && !empty($uploadResultObj->photo) && !empty($uploadResultObj->hash)) { $postUrl = 'https://api.vkontakte.ru/method/photos.saveWallPhoto?server=' . $uploadResultObj->server . '&photo=' . $uploadResultObj->photo . '&hash=' . $uploadResultObj->hash . '&gid=' . str_replace('-', '', $options['pgIntID']) . '&access_token=' . $options['vkAppAuthToken']; $response = wp_remote_get($postUrl); $resultObject = json_decode($response['body']); //prr($resultObject); if (isset($resultObject) && isset($resultObject->response[0]->id)) { return $resultObject->response[0]; } else { return false; } } } }
/** * Prepares a file for upload. To be used inside the parameters declaration for a request. * @param string $filename The file path * @param string $mimetype MIME type * @param string $postname the file name * @return string|\CURLFile */ public static function file($filename, $mimetype = '', $postname = '') { if (class_exists('CURLFile', false)) { return new \CURLFile($filename, $mimetype, $postname); } if (function_exists('curl_file_create')) { return curl_file_create($filename, $mimetype, $postname); } return sprintf('@%s;filename=%s;type=%s', $filename, $postname ?: basename($filename), $mimetype); }
private function send(BotResponse $response, $fileAttachment = false) { $this->curl->execute($this->slackUrl, $response->getPayload()); if ($fileAttachment) { $file = curl_file_create($fileAttachment); $payload = array('file' => $file, 'token' => $this->slackToken, 'channels' => $this->slackChannel); $result = $this->curl->execute('https://slack.com/api/files.upload', $payload); unlink($fileAttachment); } }
public function GetPostData($filename) { if (!$filename) { echo "The image doesn't exist " . $filename; } else { $cfile = curl_file_create($filename); $post_data = array('device_timestamp' => time(), 'photo' => $cfile); return $post_data; } }
public function actionIndex() { //目标地址 $targetUrl = str_replace($this->getCurrentDomain(), $this->targetDomain, Yii::$app->request->getHostInfo() . Yii::$app->request->getUrl()); //解析header $headerArray = []; foreach (Yii::$app->request->getHeaders() as $name => $items) { $name = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $name)))); foreach ($items as $item) { $item = 'Host' === $name ? $this->targetDomain : $item; $headerArray[] = "{$name}: {$item}"; } } //var_dump($_FILES);die; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1'); //curl_setopt($ch, CURLOPT_HTTPHEADER, ["Host: 127.0.0.1"]); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); curl_setopt($ch, CURLOPT_PROXY, 'localhost'); curl_setopt($ch, CURLOPT_PROXYPORT, 8888); //处理请求方式 if (Yii::$app->request->getIsPost()) { // $data = []; // foreach ($_FILES as $name => $files) { // if($file['tmp_name']){ // } // } curl_setopt($ch, CURLOPT_POST, true); //var_dump($_FILES);die; curl_setopt($ch, CURLOPT_POSTFIELDS, ['aaa' => curl_file_create($_FILES['a1111111']['tmp_name'], 'image/png', 'nihao'), 'aa' => 1]); //var_dump($_FILES);die; //curl_setopt( $ch, CURLOPT_POST, true ); //curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($_POST)); //curl_setopt($ch, CURLOPT_INFILESIZE, filesize($_FILES['a1111111']['tmp_name'])); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 1); // curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 24 * 3600 * 365); curl_setopt($ch, CURLOPT_HEADER, true); $result = curl_exec($ch); var_dump($_FILES); die; $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //解析头信息 foreach (explode("\r", substr($result, 0, $headerSize)) as $header) { $header = trim($header); if (strncmp($header, 'Location', 8) === 0 || strncmp($header, 'Referer', 7) === 0) { $header = str_replace($this->targetDomain, $this->getCurrentDomain(), $header); } header($header, false); } echo substr($result, $headerSize); }
/** @test */ public function fileUpload() { $file = __FILE__; if (function_exists('curl_file_create')) { $data = array('file' => curl_file_create($file)); } else { $data = array('file' => '@' . $file); } $r = $this->makeCurl()->rawPost(static::URL . '/upload.php', $data); $this->assertEquals(basename($file) . "\t" . filesize($file) . "\n", $r->body); }
/** * @return CURLFile */ public function getCURLFile() { if ($this->mimeType == null) { $finfo = new \finfo(FILEINFO_MIME_TYPE); $this->mimeType = $finfo->buffer($this->data); } $path = tempnam(sys_get_temp_dir(), 'TELEGRAM') . '.' . str_replace('/', '.', $this->mimeType); //todo fix nasty mimetype -> ext converter file_put_contents($path, $this->data); return curl_file_create($path, $this->mimeType); }
function createFile($filename, $mime) { if (function_exists('curl_file_create')) { return curl_file_create($filename, $mime); } $value = "@{$filename}"; if (!empty($mime)) { $value .= ';type=' . $mime; } return $value; }
/** * 设置文件上传 * @param string $field * @param string $path * @param string $type * @param string $name * @return $this */ public function upload($field, $path, $type, $name) { $name = basename($name); if (class_exists('CURLFile')) { $this->set('CURLOPT_SAFE_UPLOAD', true); $file = curl_file_create($path, $type, $name); } else { $file = "@{$path};type={$type};filename={$name}"; } return $this->post($field, $file); }
public function getCurlValue() { if (function_exists('curl_file_create')) { return curl_file_create($this->filename, $this->contentType, $this->postname); } $value = "@{$this->filename};filename=" . $this->postname; if ($this->contentType) { $value .= ';type=' . $this->contentType; } return $value; }
private static function getFileParam($filename) { if (function_exists('curl_file_create')) { return curl_file_create($filename); } return "@{$filename}"; }
/** * @param $url * @param $filename * @param string $fileFormName * @param array $params * @return mixed * 模拟表单提交文件 */ public static function filePost($url, $filename, $fileFormName = 'file', $params = []) { //获取文件的mime $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型 $mimeType = finfo_file($finfo, $filename); finfo_close($finfo); if ($mimeType) { $tempArr = explode(";", $mimeType); $mimeType = $tempArr[0]; } $objFile = curl_file_create($filename, $mimeType); $params[$fileFormName] = $objFile; $curl = curl_init(); if (stripos($url, "https://") !== FALSE) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); curl_setopt($curl, CURLOPT_TIMEOUT, self::$curlTimeOut); $output = curl_exec($curl); $errno = curl_errno($curl); curl_close($curl); return $output; }