PHP转换图像以添加多部分表单数据 [英] PHP Converting an Image to add Multipart Form Data
问题描述
我在使用API时遇到了一些困难.它要求我创建一个多部分表单数据图像
I am having some difficulty with an API. It has requested that I create a Multipart Form Data image
POST https://api.working.com/v1/detail/detail.example.id1/27f145d2-5713-4a8d-af64-b269f95ade3b/images/thumbnail/normal
------------------------------330184f75e21
Content-Disposition: form-data; name="image"; filename="icon.png"
Content-Type: application/octet-stream
.PNG
imagedata
Response
我正在尝试找到一些示例PHP脚本,这些脚本会将图像转换为这种格式
I am trying to find some example PHP script that will convert an image into this format
图像文件在多部分表单数据中必须具有名称图像
The image file must have the name image in the multipart form data
我正在绞尽脑汁,梳理互联网,寻找对我有用的东西.
I am racking my brain and combing the internet to find something that works for me.
有人有可以分享的摘要吗?
Does anyone have a snippet they can share please to help?
谢谢
Rob
推荐答案
下面是一个基本的curl示例,展示了如何执行此操作:
Here's a basic curl example showing how to do it:
$url = 'https://api.working.com/v1/detail/detail.example.id1/27f145d2-5713-4a8d-af64-b269f95ade3b/images/thumbnail/normal';
$data = array(
'image' => new CURLFile(
'/path/to/icon.png',
'application/octet-string',
'icon.png'
),
'some_other_field' => 'abc',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
// do something with response
// to debug, try var_dump(curl_getinfo($ch)); and var_dump(curl_error($ch));
$ch = curl_init($url);
查看 CURLFile 类以获取有关文件上传的更多信息,以及
Check out the CURLFile class for more info on file uploads, and curl_setopt()
for more request options.
这篇关于PHP转换图像以添加多部分表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!