Last active
September 30, 2024 15:58
-
-
Save arisawali2014/4ef23825b5a1d987c84b3de4b4473147 to your computer and use it in GitHub Desktop.
Upload Image to Telegraph with Curl
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 | |
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