I try to send photo via Telegram bot, so I wrote the code (a part of it):
$img_path = "https://www.example.com/path/to/file.jpg";
$method = "sendPhoto";
$arrayQuery = array(
"chat_id" => CHAT_ID,
"photo" => curl_file_create($img_path, $img_mime, $img_name),
"caption" => $caption,
"parse_mode" => "HTML",
"show_caption_above_media" => True,
);
try {
$ch = curl_init('https://api.telegram.org/bot'.BOT_TOKEN.'/'.$method);
if ($ch === false) {
throw new Exception('failed to initialize');
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrayQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
if ($res === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()),
E_USER_ERROR);
} finally {
if (is_resource($ch)) {
curl_close($ch);
}
}
So this code works fine locally, but when I upload it to remote server curl throws error 26. Can anybody help me with this?
I tried to add CURLOPT_SSL_VERIFYHOST false, CURLOPT_SSL_VERIFYPEER false but not result
The Telegraph documentation for sendPhoto says
Instead of supplying a local path and filename, you may just pass the image URL
So all you need is:
$arrayQuery = array(
"chat_id" => CHAT_ID,
"photo" => $img_path,
"caption" => $caption,
"parse_mode" => "HTML",
"show_caption_above_media" => True,
);
The problem you are experiencing, where curl_file_create
works with a URL from your local but not the remote server, may be due to security restrictions of the allow_url_fopen
setting.
From php's documentation:
This option enables the URL-aware fopen wrappers that enable accessing URL object like files.
Although this option is enabled by default, many hosting platforms will disable it as it is considered a security risk.