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 somewheresomefile.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.
3
Answers
Have you tried to send it hardcore way like this?
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
from $content and add chat_id to curl url because
After wasting a full hour on this myself years after this post I’m sharing how I got it working:
This is on PHP7, the trick seems to be two things: