How to set the content type with Zend_Http_Client on PUT?

冷暖自知 提交于 2019-12-12 19:58:59

问题


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
);

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');

This throws following exception: Unknown or erroreous cURL option 'CURLOPT_HEADER' set

How can I set the Content-Type correctly?


回答1:


Instead of using:

$client->setRawData($filePath);
$adapter->setCurlOption('CURLOPT_HEADER', "Content-Type: $mimeType");

I had to use:

$client->setRawData(file_get_contents($filePath));
$client->setHeaders('Content-Type', $mimeType);

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,
);



回答2:


You're using the CURLOPT_HEADER which is used to set CURL to report HEADERS received by the server response, to send HTTP headers:

<?php

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: $mimeType"));

?>

I dont know zend_HHTP_Client at all but try this:

$adapter->setCurlOption('CURLOPT_HTTPHEADER', array("Content-Type: $mimeType"));


来源:https://stackoverflow.com/questions/8818014/how-to-set-the-content-type-with-zend-http-client-on-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!