0

I need help converting following CURL command to PHP.

curl -X PUT -F file=@myfile.zip -F file_type=binary \
    "https://example.com/rest/api/path"

Using PHP 5.6 so 'file'=>'@'.$filename is not an option.

Mojtaba
  • 6,012
  • 4
  • 26
  • 40

1 Answers1

0

You can use following code to convert your request in to PHP:

// initialise the curl request
$request = curl_init('https://example.com/rest/api/path');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('myfile.zip') .
      ';type='     . $_FILES['file']['type']
));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($request);

// close the session
curl_close($request);

UPDATE FOR 5.6

// initialise the curl request
$request = curl_init('https://example.com/rest/api/path');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS, new CurlFile('myfile.zip', $_FILES['file']['type'], 'myfile.zip'));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($request);

// close the session
curl_close($request);
Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12