本文介绍了如何在PUT上使用Zend_Http_Client设置内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在重构curl调用Zend_Http_Client调用。这将向给定文件的CouchDB数据库发送一个PUT请求,并为_attachement设置正确的Content-Type。
Hi I'm refactoring following curl call to a Zend_Http_Client call. This will send a PUT request to a CouchDB database with the given file and set the correct Content-Type for the _attachement.
exec(
'curl -s -X PUT ' . $url ' .
'--data-binary @\'' . $filePath . '\' -H "Content-Type: ' . $mimeType . '"', $resultJson, $returnCode
);
重构到Zend_Http_Client我有以下:
Refactored to Zend_Http_Client I've got the following:
$adapter = new Zend_Http_Client_Adapter_Curl();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);
$client->setUri($url);
$client->setRawData($filePath);
$adapter->setCurlOption('CURLOPT_HEADER', "Content-Type: $mimeType");
$response = $client->request('PUT');
这会抛出以下异常:
未知或错误的cURL选项'CURLOPT_HEADER'set
This throws following exception:Unknown or erroreous cURL option 'CURLOPT_HEADER' set
如何正确设置内容类型?
How can I set the Content-Type correctly?
推荐答案
而不是使用:
$client->setRawData($filePath);
$adapter->setCurlOption('CURLOPT_HEADER', "Content-Type: $mimeType");
我不得不使用:
$client->setRawData(file_get_contents($filePath));
$client->setHeaders('Content-Type', $mimeType);
显然你不能通过setCurlOption()设置一些CURLOPT_;
Apparently you can't set some of the CURLOPT_ via setCurlOption();
$this->_invalidOverwritableCurlOptions = array(
CURLOPT_HTTPGET,
CURLOPT_POST,
CURLOPT_PUT,
CURLOPT_CUSTOMREQUEST,
CURLOPT_HEADER,
CURLOPT_RETURNTRANSFER,
CURLOPT_HTTPHEADER,
CURLOPT_POSTFIELDS,
CURLOPT_INFILE,
CURLOPT_INFILESIZE,
CURLOPT_PORT,
CURLOPT_MAXREDIRS,
CURLOPT_CONNECTTIMEOUT,
CURL_HTTP_VERSION_1_1,
CURL_HTTP_VERSION_1_0,
);
这篇关于如何在PUT上使用Zend_Http_Client设置内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!