如何将下面的curl命令转换为Laravel使用的PHP?
curl -X POST -F "images_file=@fruitbowl.jpg" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20"发布于 2017-07-17 15:00:41
看看这个包,这是一个非常简单的laravel https://github.com/ixudra/curl卷曲包装器
所以看起来就像
use Ixudra\Curl\Facades\Curl;
$response = Curl::to('https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20')
->withContentType('multipart/form-data')
->withData(['images_file' => file_get_contents("yourfile")])
->containsFile()
->post();发布于 2017-07-17 17:09:03
有一个用curl php来上传文件的功能
function uploadFileWithCURL($fullFilePath, $targetURL)
{
if (function_exists('curl_file_create')) {
$curlFile = curl_file_create($fullFilePath);
} else {
$curlFile = '@' . realpath($fullFilePath);
}
$post = array('file_contents' => $curlFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}用法:
$result = uploadFileWithCURL('path/to/your/fruitbowl.jpg','https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20');发布于 2021-06-29 11:33:41
使用Http外观并调用attach方法,该方法接受文件名及其内容
use Illuminate\Support\Facades\Http;
$response = Http::attach(
'images_file', '/home/user/fruitbowl.jpg', 'fruitbowl.jpg'
)->post('https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20'
)->json();https://laravel.com/docs/8.x/http-client#multi-part-requests
https://stackoverflow.com/questions/45137729
复制相似问题