I am trying to upload a file to a Google Signed URL with cURL in PHP.
I have the file being posted from a form and can access it with the $_FILES var.
However the file that actually gets uploaded is not the right file and I think it is to do with how I am handling the tmp file.
$file_name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
// $file = fopen($temp_name, 'r');
// $file = realpath($temp_name);
$request = curl_init($request_url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $file);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type:text/plain','Authorization:'.$token));
curl_setopt($request, CURLOPT_CUSTOMREQUEST, 'PUT');
$response = curl_exec($request);
$errors = curl_error($request);
curl_close($request);
var_dump($response);
var_dump($errors);
The following works as expected with content-type text instead of audio even though its a wav file.
curl -v -X PUT --upload-file file.wav --header "Content-Type: text/plain" request_url
Edit
I have tried this:
$file = new CurlFile($temp_name,$mime_type,$file_name);
but this just crashes my page altogether.
I think it may be to do with how I am calling the request, so I wanted to create a cURL function that I can just pass the url and data to for all my requests like so:
$file = new CurlFile($temp_name,$mime_type,$file_name);
$result = curl_request($conn,$signed_url,$file,'text/plain','PUT');
Then my function is like this:
function curl_request($conn,$request_url,$request_data,$content_type,$request_type){
$api_username = 'API username';
$stmt = $conn->prepare("SELECT * FROM config WHERE setting=:setting");
$stmt->bindParam(':setting', $api_username);
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $key=>$row) {
$username = $row['value'];
}
$api_key = 'API key';
$stmt = $conn->prepare("SELECT * FROM config WHERE setting=:setting");
$stmt->bindParam(':setting', $api_key);
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $key=>$row) {
$key = $row['value'];
}
$data = array(
'username' => $username,
'password' => $key
);
$payload = json_encode($data);
$initial_request = curl_init('https://example.com/auth');
curl_setopt($initial_request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($initial_request, CURLOPT_POSTFIELDS, $payload);
curl_setopt($initial_request, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$initial_response = curl_exec($initial_request);
$initial_errors = curl_error($initial_request);
curl_close($initial_request);
$decoded_response = (array)json_decode($initial_response);
$token = $decoded_response['token'];
$request = curl_init($request_url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type:'.$content_type,'Authorization:'.$token));
curl_setopt($request, CURLOPT_CUSTOMREQUEST, $request_type);
$response = curl_exec($request);
$errors = curl_error($request);
curl_close($request);
if(empty($errors)){
return $response;
}else{
return $errors;
}
}
This works when I had $file = fopen($temp_name, 'r');
but the file uploaded was a weird file.
Edit 2
This is what the file looks like when the person at the other end of this API tries to open it.
See Question&Answers more detail:
os