需求是这样的,需要把图片post上传到第三方的图片服务器上。使用到的方法如下:
function upload($fullName,$imgUrl){ $uploadToken = '1131112319g8sd09g80'; //token $url = 'http://img.a.com'; //第三方请求地址 $mimeBoundary = md5(microtime()); $header = array('Content-Type'=>'multipart/form-data;boundary='.$mimeBoundary); $data = array(); //需要传递的参数,这里只是用了一个密钥,fileName为需要将图片上传到的目的地址
//$fullName 为'/a/b/asfsfaf.jpg'格式
//如果有其他参数,需要像下面添加到$fields里面
$fields = array( 'secureKey'=>$uploadToken, 'fileName'=>$fullName, ); foreach ($fields as $name => $val) { array_push($data, '--' . $mimeBoundary); array_push($data, "Content-Disposition: form-data; name=\"$name\""); array_push($data, ''); array_push($data, $val); } //文件 array_push($data, '--' . $mimeBoundary); $name = 'file'; $fileName = '1.jpg'; $fileBody = file_get_contents($imgUrl);//$imgUrl为图片路径 array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$fileName\""); array_push($data, 'Content-Type: application/octet-stream'); array_push($data, ''); array_push($data, $fileBody); array_push($data, '--' . $mimeBoundary . '--'); array_push($data, ''); $body = implode("\r\n", $data); $response = request($url, 'POST', $header, $body); return $response; } function request($path, $method, $headers = null, $body = null){ $timeout = ''; $ch = curl_init($path); $_headers = array('Expect:'); if (!is_null($headers) && is_array($headers)){ foreach($headers as $k => $v) { array_push($_headers, "{$k}: {$v}"); } } $length = 0; $date = gmdate('D, d M Y H:i:s \G\M\T'); if (!is_null($body)) { if(is_resource($body)){ fseek($body, 0, SEEK_END); $length = ftell($body); fseek($body, 0); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_INFILE, $body); curl_setopt($ch, CURLOPT_INFILESIZE, $length); } else { $length = @strlen($body); array_push($_headers, "Content-Length: {$length}"); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } } else { array_push($_headers, "Content-Length: {$length}"); } // array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length)); array_push($_headers, "Date: {$date}"); curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($method == 'PUT' || $method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); } else { curl_setopt($ch, CURLOPT_POST, 0); } if ($method == 'HEAD') { curl_setopt($ch, CURLOPT_NOBODY, true); } $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); list($header, $body) = explode("\r\n\r\n", $response, 2); if ($status == 200) { if ($method == 'GET') { return $body; } else { return response($response); } } else { return false; } } function response($text){ $headers = explode(PHP_EOL, $text); $items = array(); foreach($headers as $header) { $header = trim($header); if(strpos($header, '{') !== False){ $items = json_decode($header, 1); break; } } return $items; }
upload('/a/c/d/m/adfsadfa.jpg','public/aaa.jpg');
将public/aaa.jpg上传到第三方服务器的/a/b/c/d/m/adfsadfa.jpg 下,这里的/a不一定是linux下的/目录...