Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devondahon's avatar

Upload file : convert cURL syntax to PHP syntax

How to achieve this:

curl --location --request POST '{baseUrl}/signature_requests/{signatureRequestId}/documents' \
--header 'Authorization: Bearer {apiKey}' \
--form 'file=@"/path/to/the/document/test.pdf"' \
--form 'nature="signable_document"'

Using Laravel function ? My problem is to convert --form 'file line the Laravel/PHP way.

I tried this:

    public function upload()
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer KewqE*****WWK7'
        ])->post(
            'https://api-sandbox.myapp.app/v33/documents',
            [
                'file' => readfile('/Users/me/Downloads/test.pdf'),
                'nature' => 'signable_document',
                'parse_anchors' => 'true'
            ]
        );
        return $response;
    }

But I'm getting an error with the uploaded file ("Malformed or invalid Content-Type header").

0 likes
2 replies
LaryAI's avatar
Level 58

To convert the cURL syntax to PHP syntax, you can use the curl_setopt function to set the options for the cURL request. Here's an example of how you can achieve this in PHP:

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, '{baseUrl}/signature_requests/{signatureRequestId}/documents');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
    'file' => new CURLFile('/path/to/the/document/test.pdf'),
    'nature' => 'signable_document'
]);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer {apiKey}'
]);

$response = curl_exec($curl);

curl_close($curl);

return $response;

In this example, we're using the curl_init function to initialize a new cURL session. We then use the curl_setopt function to set the options for the cURL request.

We set the URL for the request using CURLOPT_URL, and set CURLOPT_RETURNTRANSFER to true to return the response as a string. We set CURLOPT_POST to true to indicate that we're making a POST request, and use CURLOPT_POSTFIELDS to set the data to be sent in the request.

We set the file parameter to a new CURLFile object, which takes the path to the file as its argument. We set the nature parameter to 'signable_document'.

Finally, we set the Authorization header using CURLOPT_HTTPHEADER, and pass in an array with the header value.

Once we've set all the options, we execute the cURL request using curl_exec, and close the cURL session using curl_close. The response from the request is returned as a string.

Please or to participate in this conversation.