4

How to achieve an image upload via cURL?
I am using a sandbox api for convert pdf file to .txt file. If i use "https://s3.amazonaws.com/sample.pdf" path of pdf for $sourceFile variable then its working fine. But if i use system path "test.pdf" then it's not working.

I was trying to through curl_file_create, but it gives the following error:

Recoverable fatal error: Object of class CURLFile could not be converted to string in line.

I am using PHP Version 7.3.0

<?php
error_reporting(E_ALL);
$endpoint = "https://api.xxx.com/v1/jobs";
$apiKey = "GiVU******FR48H";
$sourceFile = "https://s3.amazonaws.com/sample.pdf";//new CurlFile("test.pdf");
$targetFormat = "txt";


$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);
$response = json_decode($body, true);
echo "Response:\n---------\n";
echo"<pre>";
print_r($response);
?>

Response: ---------
Array
(
    [id] => 8442473
    [key] => GiVU******FR48H
    [status] => initialising
    [sandbox] => 
    [created_at] => 2019-11-12T10:35:01Z
    [finished_at] => 
    [import] => Array
        (
            [id] => 671784
            [url] => https://s3.amazonaws.com/sample.pdf
            [status] => initialising
        )

    [target_files] => Array
        (
        )

    [target_format] => txt
    [credit_cost] => 0
)

Above example give me output as fine as well. If i replace amazonaws from system path then it doesn't gives any response.

Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56

1 Answers1

2

CURLOPT_SAFE_UPLOAD is always set to false and curl expects files to be passed as string (@/path/to/file) because of it. In your case the code sends files as objects because it's using curl_file_create() when available.

When CURLOPT_SAFE_UPLOAD is set to false, you should not use curl_file_create() (it's flag for backwards compatibility).

Also, try to set CURLOPT_POSTFIELDS after CURLOPT_SAFE_UPLOAD (see here why).

PHP 5.x is not supported anymore and I suggest to get rid of the old syntax (@/path/to/file and CURLOPT_SAFE_UPLOAD = false) and always use curl_file_create().

Luxian
  • 676
  • 7
  • 14
  • 1
    I see multiple issues: 1) You do not set `@` in front of the path, 2) `CURLOPT_SAFE_UPLOAD` is set after `CURLOPT_POSTFIELDS` (you missed that part from my answer). Try to fix those and then update with the result. – Luxian Nov 11 '19 at 09:11
  • i have implemented both methods and updated code. I haven't received any error but i didn't get any response from api. I used `new CurlFile` class method instead of @filename because `@filename` has been deprecated. When i use `amazonaws` path then it provides response. – Nirdesh Kumawat Nov 11 '19 at 09:46
  • I run your script and get the message - [message] => the size of file exceeds the maximum file size cap for the current plan. And I delete this option - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); – Dmitry Leiko Nov 11 '19 at 13:17
  • @Martin since you switched to new upload method (`CURLFile`), you do not need to set `CURLOPT_SAFE_UPLOAD` anymore. Remove it. I'm not really sure what's happening in your case, can you post the response in both cases as well? I suspect that the `amazonaws` request just send the URL as a string. – Luxian Nov 12 '19 at 10:07
  • @Dmitry that looks like a server limit, the upload script is probably working. – Luxian Nov 12 '19 at 10:08
  • @Luxian I updated code and you also can check it just copy and paste. If it's server limit then why it return response when i use `amazonaws` path and in case of system pdf doesn't return response? – Nirdesh Kumawat Nov 12 '19 at 10:48
  • @Martin the server limit was a reply to @Dmitry. In the current code I see that you are still using `curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);` - You do not need that if you use `CurlFile`. Also, you are send an URL as text field, it works differently than a file upload. Let me know how it goes after you remove that line. – Luxian Nov 13 '19 at 20:34
  • @Luxian after remove `curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);` it doesn't give any error or any response. – Nirdesh Kumawat Nov 14 '19 at 05:14
  • @Martin copy pasted the code and run it on my machine and seems to work (I get file size too big error - I assume upload worked). You can find it here: https://gist.github.com/Luxian/70222e7089c21d880278604a806d466c with some comments where I changed. Also, change your API key, it's now public. – Luxian Nov 15 '19 at 08:39
  • @Luxian Just i copy and paste. I run it with small size pdf but it doesn't give any error or any response. – Nirdesh Kumawat Nov 15 '19 at 09:01
  • @Martin can you please check this answer on how to get more debug info: https://stackoverflow.com/a/14436877/1050554 – Luxian Nov 15 '19 at 16:35