将CURL命令转换为PHP以便与Laravel一起使用
问题描述:
如何将以下curl命令转换为与Laravel一起使用的PHP?将CURL命令转换为PHP以便与Laravel一起使用
curl -X POST -F "[email protected]" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20"
答
看看这个包,这是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();
+0
我得到语法错误,意外的' - >'(T_OBJECT_OPERATOR) - > withData行 – DanielA
+0
是的,它错过了一个大括号,我将数组转换为短语法,所以它更清晰对不起:) –
答
还有就是上传文件中的函数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');
你到目前为止试过了什么? – Jerodev