2015-07-20 41 views
2

我想在我的本地主机SendPhotowebhook。该图片不在电报服务器中。所以我需要通过多段头来上传它。电报SendPhoto方法does not工作

试图代码:

$file=fopen("Untitled.png","rb"); 
$cont=fread($file,filesize("Untitled.png")); 
$headers=array("Content-type: multipart/form-data"); 
$postfields = array("chat_id" => "108432389", "photo" => "$file"); 
$ch = curl_init(); 
$options = array(
    CURLOPT_URL => "https://api.telegram.org/bot(Token)/SendPhoto", 
    CURLOPT_HEADER => true, 
    CURLOPT_POST => 1, 
    CURLOPT_POSTFIELDS => $postfields, 
    CURLOPT_INFILESIZE => filesize("Untitled.png"), 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_HTTPHEADER => $headers 
); 
curl_setopt_array($ch, $options); 
curl_exec($ch); 

但它不能SendPhoto。

我已经在不同的网站上找到了解决方案,但是他们的代码与我的代码相同。

为什么不能正常工作?

+0

我觉得这个解决方案可以解决您的问题,帮助http://stackoverflow.com/a/32844167/5348805 – Ars

回答

1

这是我的PHP函数,它可以完成这项工作。

sendPicture($id, "picture.png"); 


function sendPicture($_chatID, $file_on_server){ 

    $target_url = 'https://api.telegram.org/**<YOUR TOKEN HERE>**/sendphoto'; 

    $file_name_with_full_path = realpath('./'.$file_on_server); 

    $post = array(
     'chat_id' => $_chatID, 
     'photo'  => '@'.$file_name_with_full_path 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$target_url); 
    curl_setopt($ch, CURLOPT_POST,1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $result=curl_exec ($ch); 
    curl_close ($ch); 
    echo $result; 


} 
+0

谢谢@Markus但它不能sendphoto过。 '{“ok”:false,“error_code”:400,“description”:“错误:错误的请求:错误的持久file_id指定:包含错误的字符或长度错误”} – Alireza

+0

@Alireza:在我看来,与请求的编码。 Telegram bot API仅使用UTF-8。所以你可以尝试一下,把你的编码设置为UTF-8:iconv(mb_detect_encoding($ text,mb_detect_order(),true),“UTF-8”,$ text); – Markus

+0

该照片不在电报服务器中。 我们应该先上传。但我们没有! – Alireza