CURL PHP 发送图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3433542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:45:12  来源:igfitidea点击:

CURL PHP send image

phpcurl

提问by xxxZonexxx

It's trivial to get image from server but I think about something different. It is crazy question but... Is it possible to send file (image) to a server but not using form upload or ftp connection? I want to send a request to eg. http://www.example.com/file.phpwith binary content. I think I need to set Content-type header image/jpeg but how to add some content to my request?

从服务器获取图像很简单,但我想到了一些不同的东西。这是一个疯狂的问题,但是...是否可以将文件(图像)发送到服务器但不使用表单上传或 ftp 连接?我想向例如发送请求。http://www.example.com/file.php带有二进制内容。我想我需要设置 Content-type 标题图像/jpeg 但如何向我的请求添加一些内容?

回答by Andy Lin

there are multiple ways to use curl to upload image files, e.g.:

有多种使用 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);

you can check the examples at: http://au.php.net/manual/en/function.curl-setopt.php

您可以在以下位置查看示例:http: //au.php.net/manual/en/function.curl-setopt.php

回答by VolkerK

see http://docs.php.net/function.curl-setopt:

http://docs.php.net/function.curl-setopt

CURLOPT_POSTFIELDSThe full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
CURLOPT_POSTFIELDS要在 HTTP“POST”操作中发布的完整数据。要发布文件,请在文件名前加上 @ 并使用完整路径。这既可以作为 urlencoded 字符串传递,如 'para1=val1¶2=val2&...' 也可以作为以字段名称作为键和字段数据作为值的数组。如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。

回答by Husam

The only code worked for me with PHP 7.0

唯一的代码适用于PHP 7.0

$file = new \CURLFile('@/path/to/image.jpeg'); //<-- Path could be relative
$data = array('name' => 'Foo', 'file' => $file);

$ch = curl_init();
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);

Thanks to @AndyLin for his answer and this source.

感谢@AndyLin 的回答和这个来源

回答by Shubham

VolkerK is completely right but my experience suggest that sending a file "@" operator will only work with arrays.

VolkerK 是完全正确的,但我的经验表明,发送文件“@”运算符仅适用于数组。

$post['file'] = "@FILE_Path"

Now you can send the file using CURLOPT_POSTFIELDS

现在您可以使用发送文件 CURLOPT_POSTFIELDS