0

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.

Rahul Bali
  • 692
  • 10
  • 28
  • what is it with "images of" code lately?? – Funk Forty Niner Nov 05 '15 at 03:47
  • Should I remove it.? – Rahul Bali Nov 05 '15 at 03:47
  • you should include your code in text format, should visitors to the question have to fix something, rather than retyping everything. – Funk Forty Niner Nov 05 '15 at 03:48
  • TBH, I don't know about CURL very much, but I do know about uploading files in PHP. What I know is that it requires a valid enctype when handling files. Whether CURL does this automatically, I couldn't say, but as I went through some documentation, it looks as if it does. Have a look at this Q&A on Stack http://stackoverflow.com/questions/4223977/send-file-via-curl-from-form-post-in-php see if that helps. – Funk Forty Niner Nov 05 '15 at 03:55
  • That is good. But my question here is little different than that. – Rahul Bali Nov 05 '15 at 03:58
  • Ah ok. Well let's see if someone else picks up on your question. – Funk Forty Niner Nov 05 '15 at 04:00
  • Actually, I found a mistake of yours: you pass `$filePhoto` to **CURLOPT_POSTFIELDS**, but you should pass a `$content` instead. – retif Nov 10 '15 at 11:36
  • Yes, and I used $content to CURLOPT_POSTFIELDS, this shows the error 400. As I told in the question, about the second error. – Rahul Bali Nov 10 '15 at 16:44

3 Answers3

0

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.

retif
  • 1,495
  • 1
  • 22
  • 40
0

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

0

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:

  • Content-Type:multipart/form-data
  • Using \CurlFile
Mark
  • 61
  • 1
  • 3