Skip to content

Instantly share code, notes, and snippets.

@arisawali2014
Last active September 30, 2024 15:58
Show Gist options
  • Save arisawali2014/4ef23825b5a1d987c84b3de4b4473147 to your computer and use it in GitHub Desktop.
Save arisawali2014/4ef23825b5a1d987c84b3de4b4473147 to your computer and use it in GitHub Desktop.
Upload Image to Telegraph with Curl
<?php
public function uploadMedia($file)
{
$target_url = "https://telegra.ph/upload";
$file_name_with_full_path = $file;
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('file' => $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode(
$res,
true
);
if (isset($res[0]['src'])) {
return 'https://telegra.ph' . $res[0]['src'];
}
}
@Livzjasteb
Copy link

Uploading IMG-20240930-WA0025.jpg…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment