I'm a building a RPC API using Zend Framework 2 and Apigility by Zend Framework. For testing, I use the chrome extension Postman REST-Client.
I can do POST requests without problems when I use Postman. But my code doesn't work.
$client = new \Zend\Http\Client();
$client->setUri($uri)
->setMethod('POST')
->setParameterPost(
array(
'file' => '/home/user/Downloads/file.csv'
)
);
$headers = new \Zend\Http\Headers();
$headers->addHeaders(array(
'Accept' => 'application/json;',
));
$client->setHeaders($headers);
$client->setStream();
$response = $client->send();
$file = '/home/user/Downloads/file.csv';
$file2 = '/home/user/Downloads/file2.csv';
copy($response->getStreamName(), $file);
$fp = fopen($file2, "w");
stream_copy_to_stream($response->getStream(), $fp);
$client->setStream($file);
$responce = $client->send();
echo $responce->getBody();
I tried to pass other headers Content-Type
, but it leads to Fatal error. What do I need to pass the headers to make it work?
You have to set your Content-Type
header to multipart/form-data
. The Postman add-on you are using in Chrome does this automatically for file uploads.
So set your headers like this:
$headers->addHeaders(array(
'Accept' => 'application/json',
'Content-Type' => 'multipart/form-data'
));
If that doesn't work please be more specific about the fatal error you get.