文件上传CURL命令到PHP CURL



我有CURL命令,我需要在php页面中使用他来更改它。我对此一无所知。命令:

curl --request POST 
--url 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg' 
--header 'authorization: Bearer BEARER_TOKEN_HERE' 
--header 'content-type: image/jpeg'  
--data "@/path/to/local-file.jpg"

对于您的curl命令,请尝试以下PHP:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '@' .realpath('/path/to/local-file.jpg')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Authorization: Bearer BEARER_TOKEN_HERE';
$headers[] = 'Content-Type: image/jpeg';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
?>

如果你有时间的话,也请研究Abronsius教授提供的参考资料付出努力是值得的

最新更新