如何使用PHP cURL发送带有正确Content-Type的图像? [英] How to use PHP cURL to send images with the correct Content-Type?
问题描述
目标:
我想使用multipart / form-data发送多个图片。以下是我的代码片段。
问题:
,多部分附件由正确的MIME头Content-Type:image / jpeg发送,但在另一台PC上,MIME头是Content-Type:application / octet。
问题:如何强制cURL设置正确的Content- MIME内容的类型标头?
$ ch = curl_init
$ params = array('name'=>'@D:\globe.jpg');
$ base_url =https://example.com。?.varEncode($ test_data);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_VERBOSE,TRUE);
curl_setopt($ ch,CURLOPT_URL,$ base_url);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ params);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ result = curl_exec($ ch);
使用CURLOPT_HTTPHEADER选项:
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(Content-Type:image / jpeg));
因此指定专门用于文件上传的Content-Type标头,使用:
$ params = array('name'=>'@ D:\globe.jpg; type = image / jpeg');
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ params);
Objective:
I want to send multiple images using multipart/form-data. Below is a snippet of my code.
Problem:
On one PC, the multipart attachment is sent by the correct MIME header Content-Type: image/jpeg, but on another PC, the MIME header is Content-Type: application/octet.
Question:
How can I force cURL to set the correct Content-Type header for the MIME content?
$ch = curl_init();
$params = array('name' => '@D:\globe.jpg');
$base_url = "https://example.com"."?".varEncode($test_data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL,$base_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
Use the CURLOPT_HTTPHEADER option:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));
So specify Content-Type headers specifically for file uploads, use:
$params = array('name'=>'@D:\globe.jpg;type=image/jpeg');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
这篇关于如何使用PHP cURL发送带有正确Content-Type的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!