Last active
May 20, 2023 20:21
-
-
Save slav123/cf7779073ed16914b2ec9368140e067a to your computer and use it in GitHub Desktop.
PHP CURL POST query multipart/form-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "API_ENDPOINT", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => array('file' => new CURLFile('path/to/file')), | |
CURLOPT_HTTPHEADER => array( | |
"Content-Type: multipart/form-data" | |
), | |
)); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, | |
array("Content-type: application/json")); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); | |
$json_response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ( $status != 201 ) { | |
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); | |
} | |
curl_close($curl); | |
return json_decode($json_response, true); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment