POST卷曲不起作用。但可以用表格来完成

时间:2015-07-22 15:18:01

标签: php curl


我想POST请求到这个网站: https://api.telegram.org/myToken/sendphoto
我为此创建了一个表单:

<!DOCTYPE html>
<html>
<body>
<form action="http://api.telegram.org/MYTOKEN/sendphoto" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="photo" id="photo">
    <input type="hidden" value="108432389" name="chat_id">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html> 

它非常好用。发送了以下标题:

-----------------------------25522111424583
Content-Disposition: form-data; name="photo"; filename="Untitled.png"
Content-Type: image/png

 PNG

我用cURL编写了以下脚本:

<?php
$ch = curl_init('https://api.telegram.org/TOKEN/SendPhoto');
$cfile = new CURLFile('ur.png','image/png','photo');
$data = array('chat_id' => '108432389', 'photo' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res=curl_exec($ch);
echo $res;
?>

但它不起作用,我的错在哪里?

1 个答案:

答案 0 :(得分:0)

它解决了。
这一行:

$cfile = new CURLFile('ur.png','image/png','photo');

应改为:

$cfile = new CURLFile(realpath('ur.png'),'image/png','photo.png');