javascript 尝试通过php发送图片到telegraph

b5lpy0ml  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(86)

我正在尝试通过php发送图片到telegraph,但它不发送...
(The base64文件类型的图像)

    • 联属萨摩亚**
var imagg;
function convertCanvasToImage(canvas) { // canvas = data:image/png;base64,iVBORw0....
  var image = new Image();
  image.src = canvas.toDataURL("image/png");
  imagg = image.src;
  return image;
 }        

setTimeout(function() {
$.post("telegram.php", {uu: imagg}, function(data) {
                // returned from php
             });
}, 5000);
    • PHP代码:**
<?php
$uu = $_GET['uu'];

$apiToken = "TOKEN.....";

$data = [
    'chat_id' => '@CHAT_ID...',
    'photo' => $uu,
    'caption' => 'Markdown'

];

$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendPhoto?" . http_build_query($data) );
?>
ntjbwcob

ntjbwcob1#

把你的php代码改成这个,它应该可以工作

<?php
$uu = $_GET['uu'];//or $_POST['uu'];

$photoData = explode(',', $uu)[1];
$photoDataDecoded = base64_decode($photoData);

$apiToken = "TOKEN.....";

$data = [
    'chat_id' => '@CHAT_ID...',
    'photo' => new \CURLStringFile($photoDataDecoded, 'file.png'),
    'caption' => 'Markdown'
];

$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendPhoto?" . http_build_query($data) );
?>

相关问题