Skip to content
Advertisement

File upload CURL command to PHP Curl

I have CURL command I need to use him in php page how I can change it. I have no idea about it. The command :

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"

Advertisement

Answer

For your curl command, Please try this 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);
?>

Please also study the reference provided by Professor Abronsius if you have time. It’s worth the effort.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement