tom
tom

Reputation: 8359

How to set the content type with Zend_Http_Client on PUT?

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?

Upvotes: 2

Views: 4598

Answers (2)

tom
tom

Reputation: 8359

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

Upvotes: 2

Prof
Prof

Reputation: 2908

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

Upvotes: 1

Related Questions