php - 在 php curl 中发送以 "@"开头的字符串作为字符串

标签 php curl telegram-bot

我正在尝试使用 CURL POST 方法通过 php 发送类似 "@mystr" 的字符串,但是如果我只使用以下代码发送:

        $url_str = $url->getUrl();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_str);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:multipart/form-data']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $out = curl_exec($ch);
        curl_close($ch);

它会抛出一个错误:

Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead...

注意: $fields 是这样的:

$fields=['chat_id'=>'@someusername','text'=>'hello'];

这是用于 telegram bot api 的,如果我在 GET 方法中使用 %40 而不是 @ 就可以了,telegram 会将我的消息发送到我的 channel ,但我必须使用POST方法。

是否可以在 CURL 中发送以 @ 开头的字符串?

最佳答案

我在这里找到了解决方案: http://php.net/manual/en/function.curl-setopt.php

CURLOPT_SAFE_UPLOAD

TRUE 表示禁用对在 CURLOPT_POSTFIELDS 中上传文件的 @ 前缀的支持,这意味着以 @ 开头的值可以作为字段安全地传递。 CURLFile 可用于上传。

在 PHP 5.5.0 中添加,默认值为 FALSE。 PHP 5.6.0 将默认值更改为 TRUE。

    $url_str = $url->getUrl();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, TRUE); //<- HERE
    curl_setopt($ch, CURLOPT_URL, $url_str);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:multipart/form-data']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $out = curl_exec($ch);
    curl_close($ch);

关于php - 在 php curl 中发送以 "@"开头的字符串作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33743062/

相关文章:

python - 保持 python telegram bot 运行

PHP 和 MySQL - 无法从查询中正确分配对象的字段值

php - 如何使用 JMS Serializer 处理递归对象

php - 尝试在 wordpress $wpdb->prepare 上使用 LIKE Mysql

PHP Include 在子目录中不起作用?

java - 为 Java/Spring RESTful 服务创建客户端

linux - bash | curl | curl 2 个 URL 然后停止

php - 使用 CURL - 发布和重定向帮助

javascript - 游戏的每回合超时逻辑

python-3.x - 尝试编辑电报消息时出错