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