Skip to content

Instantly share code, notes, and snippets.

@arisawali2014
Last active February 25, 2024 07:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment