0

我正在尝试构建一个电报机器人,但问题与更新的 php 5.6 导致的 php 函数变化有关。

下面是我找到的基本代码,可适应 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);

这是我得到的错误:

不推荐使用:curl_setopt():不推荐使用 @filename API 进行文件上传。请在第 53 行的 C:\xampp 某处\somefile.php 中使用 CURLFile 类

当我在第 53 行将 $content 更改为 $filePhoto 时。它运行并且 Telegram 服务器以 JSON 格式发送消息。服务器回复:

"{"ok":false,"error_code":400,"description":"Error: Bad Request: there is no photo in request"}"

我已经在互联网上搜索了几个小时,寻找解决方案。顺便说一句,我为我正在使用的 PHP 5.6 建议了两种方法,它在第 39、40 行。

如果您遇到这种情况或其他情况,请帮助我。谢谢你。

4

3 回答 3

0

你应该删除

'chat_id' => "@BugTheInternet",

从 $content 并将 chat_id 添加到 curl url 因为

PHP 的 cURL 库在尝试使用包含以“@”开头的值的数组时返回错误消息“创建表单发布数据失败”。如果将数组更改为 URL 编码格式的字符串,则不会出现问题。参考:https ://bugs.php.net/bug.php?id=50060

于 2016-01-29T20:25:50.613 回答
0

在这篇文章发布多年后,我自己浪费了整整一个小时后,我分享了我是如何让它工作的:

  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) );

这是在 PHP7 上,诀窍似乎是两件事:

  • 内容类型:multipart/form-data
  • 使用 \CurlFile
于 2020-01-04T10:39:46.403 回答
0

您是否尝试过像这样发送硬核方式?

$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);

首先,我在发送消息时也为 cURL 使用了 POSTFIELDS和其他正确的东西,但它对我不起作用。所以我像上面的例子一样对它进行了硬核,它就起作用了。

于 2015-11-10T11:27:03.640 回答