Laravel项目1控制器如何将文件转发到API-2
Input::file('songFile')->move("/tmp", $newname);
我用这个函数存储了tmp位置,那么如何使用这个tmp位置文件转发?
发布于 2020-08-25 14:53:15
为了将文件发布到API端点,您可以遵循以下代码。
try{
$path = 'var/www/html/myproject/public/file.txt';//your file path
if (!empty($path) && file_exists($path)) {
$guzzleResponse = $client->post($api_url, [
'multipart' => [
[
'name' => 'file',// it is the name as specfied in the payload of api_url
'contents' => fopen($path, 'r')// or you can use file_get_contents()
]
],
'headers' => $headers
]);
}
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody());// whatever you want to do with response
}
}catch(RequestException $e){
return $e; //You can also handle specific status codes here using eg $e->getResponse()->getStatusCode() == '400'
}
同样,$headers也可以是这样的
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '. $userToken,
]
在Guzzle中查看更多信息
https://stackoverflow.com/questions/63559380
复制相似问题