从服务器获取图像很简单,但我考虑的是不同的东西。这是个疯狂的问题但是..。是否可以将文件(图像)发送到服务器,但不使用表单上传或ftp连接?我想发送一个请求到eg。包含二进制内容的http://www.example.com/file.php。我想我需要设置内容类型头部image/jpeg,但是如何在我的请求中添加一些内容呢?
发布于 2010-08-08 16:16:39
使用curl上传图像文件有多种方式,例如:
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
//CURLOPT_SAFE_UPLOAD defaulted to true in 5.6.0
//So next line is required as of php >= 5.6.0
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
您可以在http://au.php.net/manual/en/function.curl-setopt.php上查看示例
发布于 2020-09-18 22:59:31
因为某些原因,Andy Lin使用的方法对我不起作用,所以我找到了这个方法:
function makeCurlFile($file){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
return $output;
}
您可以通过将值关联到$data有效负载中的键来发送其他内容,而不仅仅是文件,如下所示:
$ch = curl_init("https://api.example.com");
$mp3 =makeCurlFile($audio);
$photo = makeCurlFile($picture);
$data = array('mp3' => $mp3, 'picture' => $photo, 'name' => 'My latest single',
'description' => 'Check out my newest song');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
}
curl_close ($ch);
我认为这是由于一些API出于安全原因而不支持旧的方式。
发布于 2021-02-11 21:53:24
我使用这种方法从HTML表单发送照片
$ch = curl_init();
$cfile = new CURLFile($_FILES['resume']['tmp_name'], $_FILES['resume']['type'], $_FILES['resume']['name']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
https://stackoverflow.com/questions/3433542
复制相似问题