如何使用PHP cURL发送带有正确Content-Type的图像? [英] How to use PHP cURL to send images with the correct Content-Type?

查看:258
本文介绍了如何使用PHP cURL发送带有正确Content-Type的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:



我想使用multipart / form-data发送多个图片。以下是我的代码片段。



问题:



,多部分附件由正确的MIME头Con​​tent-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆