简体   繁体   English

CURL PHP 发送图片

[英]CURL PHP send image

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?这是一个疯狂的问题,但是......是否可以将文件(图像)发送到服务器但不使用表单上传或 ftp 连接? I want to send a request to eg.我想向例如发送请求。 http://www.example.com/file.php with binary content. http://www.example.com/file.php带有二进制内容。 I think I need to set Content-type header image/jpeg but how to add some content to my request?我想我需要设置 Content-type header image/jpeg 但是如何在我的请求中添加一些内容?

there are multiple ways to use curl to upload image files, eg:有多种使用 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

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

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. CURLOPT_POSTFIELDS要在 HTTP“POST”操作中发布的完整数据。 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&para2=val2&...' or as an array with the field name as key and field data as value. 这既可以作为 urlencoded 字符串(如 'para1=val1&para2=val2&...')传递,也可以作为以字段名称作为键和字段数据作为值的数组传递。 If value is an array, the Content-Type header will be set to multipart/form-data. 如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。

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 的回答和这个来源

The method used by Andy Lin didn't work for me for some reason so I found this method: Andy Lin 使用的方法由于某种原因对我不起作用,所以我找到了这个方法:

function makeCurlFile($file){
    $mime = mime_content_type($file);
    $info = pathinfo($file);
    $name = $info['basename'];
    $output = new CURLFile($file, $mime, $name);
    return $output;
}

You can send other stuffs, not just files by associating the value to a key inside the $data payload as shown below:您可以通过将值关联到 $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);

I am thinking this is due to the fact that some API doesn't support the old way of doing it for security reasons.我认为这是由于某些 API 出于安全原因不支持旧的方式。

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发送文件

I used this method of sending a photo from the HTML form我使用这种从 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM