如何使用 zend HttpClient 上传文件?
How to upload a file using zend HttpClient?
我正在使用 HTTP 客户端。如何在发送 post 请求时附加文件?
这是我的代码:
function executePost($postdata = [], $full_file_path = ''){
self::$client = new Zend\Http\Client(null,
['timeout' => 30] // updated to 30 sec
);
self::$client->setEncType(Zend\Http\Client::ENC_URLENCODED);
self::$client->setUri($url);
self::$client->setMethod($method);
self::$client->setParameterPost($postdata);
$response = $client->send();
Debug::dump($response->getContent(),$label='Response',$echo=true);
}
我试过 $postdata['file_contents'] = '@'.$full_file_path;
但没有用。
有人知道如何使用 post 数据附加文件吗?
提前致谢。
尝试使用名为 setFileUpload
的指定方法,而不是使用该数据数组。所以在你的例子中它可能看起来像这样:
$client = new \Zend\Http\Client(null, ['timeout' => 30]);
$client->setUri($url);
$client->setFileUpload($full_file_path, 'file_contents');
$client->setMethod('POST'); // this must be either POST or PUT
$response = $client->send();
setFileUpload
还应该设置 enc 类型。
有一个与此相关的文档部分 here。
我正在使用 HTTP 客户端。如何在发送 post 请求时附加文件?
这是我的代码:
function executePost($postdata = [], $full_file_path = ''){
self::$client = new Zend\Http\Client(null,
['timeout' => 30] // updated to 30 sec
);
self::$client->setEncType(Zend\Http\Client::ENC_URLENCODED);
self::$client->setUri($url);
self::$client->setMethod($method);
self::$client->setParameterPost($postdata);
$response = $client->send();
Debug::dump($response->getContent(),$label='Response',$echo=true);
}
我试过 $postdata['file_contents'] = '@'.$full_file_path;
但没有用。
有人知道如何使用 post 数据附加文件吗?
提前致谢。
尝试使用名为 setFileUpload
的指定方法,而不是使用该数据数组。所以在你的例子中它可能看起来像这样:
$client = new \Zend\Http\Client(null, ['timeout' => 30]);
$client->setUri($url);
$client->setFileUpload($full_file_path, 'file_contents');
$client->setMethod('POST'); // this must be either POST or PUT
$response = $client->send();
setFileUpload
还应该设置 enc 类型。
有一个与此相关的文档部分 here。