Last active
November 21, 2024 09:52
-
-
Save supernovaplus/4e1056aa928c273dc82a449681daee61 to your computer and use it in GitHub Desktop.
PHP - Send file to Discord via Webhook
This file contains hidden or 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 | |
//from https://stackoverflow.com/questions/65133851/uploading-file-to-discord-with-webhook-in-php | |
//file is being called by a POST request in my case | |
//print_r for debugging | |
//default max size discord accepts is 8MB | |
echo "<pre>"; | |
$webhookurl = "https://discord.com/api/webhooks/xxxx/xxxxxxx"; | |
print_r($_FILES); | |
$json_data = [ | |
"content" => "file uploaded - " . $_FILES["file"]["name"], | |
"tts" => "false", | |
"file" => curl_file_create($_FILES["file"]["tmp_name"], $_FILES["file"]["type"], $_FILES["file"]["name"]) | |
]; | |
print_r($json_data); | |
$curl = curl_init( $webhookurl ); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds | |
curl_setopt($curl, CURLOPT_POST, 1); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data); | |
$returned_data = curl_exec( $curl ); | |
curl_close( $curl ); | |
echo "---response---\n"; | |
print_r(json_decode($returned_fileName)); | |
echo "</pre>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's been like 1 hours and a half that I was searching for a solution like this one. Thanks a lot !