I am trying to build a telegram bot, but the trouble is associated with the change in php functions due newer php 5.6.
Below is the basic code for it I found, accommodating changes in php 5.6.
#$filePhoto = curl_file_create($filepath, 'image/jpg', 'heInternet'); //**LINE 39**
$filePhoto = new CURLFile($filepath, 'image/jpg', 'heInternet'); //**LINE 40**
//$texto = $_POST['msg'];
$content = array(
'chat_id' => "@BugTheInternet",
'photo' => $filePhoto
);
//curl required to post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // required as of PHP 5.6.0
curl_setopt($ch, CURLOPT_POSTFIELDS, $filePhoto); //**LINE 53**
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
Here is the Error I get:
Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in C:\\xampp somewhere\\somefile.php on line 53
When I change the $content to $filePhoto in line 53. It runs and Telegram server sends a messages in JSON. Server Reply:
"{"ok":false,"error_code":400,"description":"Error: Bad Request: there is no photo in request"}"
I have searched internet for hours, finding solutions. BTW, two ways suggested for PHP 5.6 that I am using, it is in line 39, 40.
Please help me if you have come across this or otherwise. thank you.
Have you tried to send it hardcore way like this?
$ch = curl_init("https://api.telegram.org/bot<token>/sendPhoto&chat_id=<chatID>&photo=<path/to/your/image>");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);
curl_close ($ch);
Firstly I used POSTFIELDS and other correct stuff for cURL as well when sending message, but it wouldn't work for me. So I hardcored it like example above and it just worked.
you should remove
'chat_id' => "@BugTheInternet",
from $content and add chat_id to curl url because
PHP's cURL library dies returning the error message "failed creating formpost data" when trying to use an array that contains a value starting with '@'. If the array is changed to a string in URL encoded like format, the problem does not occur. refrence : https://bugs.php.net/bug.php?id=50060
After wasting a full hour on this myself years after this post I'm sharing how I got it working:
function json($url, array $fields) {
$ch = curl_init($url);
$ok = 1;
$ok &= curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
$ok &= curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$ok &= curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$ok &= curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$ok &= curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Accept: application/json"
]);
$ok &= curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ok &= curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$ok &= curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:multipart/form-data"]);
if ($ok !== 1) {
user_error("curl_setopt failed");
}
$res = curl_exec($ch);
if ($res === false) {
var_dump($res);
user_error(curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($res === false) {
user_error('curl_exec fail');
}
$json = json_decode($res, true);
if (! is_array($json)) {
print_r($res);
user_error("http_json::failed decoding");
}
return $json;
}
$f = new \CurlFile("/path/to/file.jpg", 'image/jpeg', 'filename.jpg');
$args = [
"photo" => $f,
"chat_id" => $group,
"caption" => "Hello world"
];
$url = "https://api.telegram.org/bot$token/sendPhoto";
var_dump( json($url, $args) );
This is on PHP7, the trick seems to be two things:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.