First issue fixed. then i want to upload file to 3rd party server using Guzzle.
Orginal Curl code
<?php
if (isset($_POST['btnUpload']))
{
$message='';
$url = "https://upload.dyntube.com/v1/videos";
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$filetype = $_FILES['file']['type'];
if ($filedata != '')
{
$headers = array(
'Content-type: multipart/form-data',
'Authorization: Bearer {{token}}',
);
$postfields = array("projectId" => "{{projectId}}",'file' => new CURLFile(realpath($filedata),$filetype,$filename));
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
$message= curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
if ($info['http_code'] == 200)
$message = "File uploaded successfully";
}
else
{
$message = curl_error($ch);
}
curl_close($ch);
}
else
{
$message = "Please select the file";
}
echo $message;
}
?>
Error Message
{message: "A 'contents' key is required", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\wamp64\www\lms\vendor\guzzlehttp\psr7\src\MultipartStream.php"
line: 86
message: "A 'contents' key is required"
trace: [{file: "C:\wamp64\www\lms\vendor\guzzlehttp\psr7\src\MultipartStream.php", line: 73,…},…]
Guzzle Code
$token = "eyJhbGciOiJIUz"
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$filetype = $_FILES['file']['type'];
if ($filedata != '') {
$headers = array(
'Content-type: multipart/form-data',
'Authorization: Bearer ' . $token,
);
$postfields = array(
"projectId" => "TSYRQJGuLrECoVwNErgjlQ",
'file' => new \CURLFile(realpath($filedata), $filetype, $filename)
);
} else {
$message = "Please select the file";
}
$file = "https://picsum.photos/id/237/200/300";
$client = new Client();
$client->request('POST', '/post', [
'multipart' => [
[
'name' => 'upload',
'contents' =>fopen($file,'r'),
'filename' => 'fl'
],
[
'name' => 'Authorization',
'content' => 'Bearer ' . $token,
],
[
'name' => 'projectId',
'contents' => "TYRQJGuLrECoVwNErgjlQ"
],
]
]);
}