Skip to content

Instantly share code, notes, and snippets.

@azhar25git
Last active August 1, 2023 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azhar25git/a6c807ff7a69681d425b38820dbd9d1a to your computer and use it in GitHub Desktop.
Save azhar25git/a6c807ff7a69681d425b38820dbd9d1a to your computer and use it in GitHub Desktop.
Send file with POST request via CURL PHP 7
<?php
// You have to enable PHP CURL extension
//Initialise the cURL var
$ch = curl_init();
// Path of the file
$file_uri = realpath('cat.zip');
// Creates a CURLFile object, used to upload a file with CURLOPT_POSTFIELDS.
$file_to_send = curl_file_create($file_uri, '', '');
// URL to send POST request
$url = "https://hooks.zapier.com/hooks/catch/4797032/788klv/";
// Get the response from cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set the Url
curl_setopt($ch, CURLOPT_URL, $url);
// Create a POST array with the file in it
$postData = array(
'file' => $file_to_send,
// add any field and value to send
);
// Set POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Close connection
curl_close ($ch);
// Output server response
echo $server_output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment