Tumblr API 从 HTML Canvas 发布照片
我在尝试使用代码将 HTML 画布作为照片发布到 Tumblr 时遇到一些问题。我使用的是 PHP,服务器端的代码如下:
if(isset($_POST['postphoto']) and $_SESSION['loggedin']) {
$title = $_POST['title'];
$caption = $_POST['caption'];
tags = $_POST['tags'];
$imageData = $_POST['imageData'];
$imageData = substr($imageData,strpos($imageData,",")+1);
$imageData = str_replace(' ','+',$imageData);
# Set access token
$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array(
'type' => 'photo',
'generator' => 'AppName',
'data' => $imageData,
'name' => $title,
'caption' => $caption,
'tags' => $tags
);
$response = $tumblr->fetch('http://www.tumblr.com/api/write', $data);
if($response['successful']) {
echo "Update successful!<br><br>";
} else {
echo "Update failed. {$response[body]}<br><br>";
}
}
在 JavaScript 端,我有以下内容:
var imgData = canvas.toDataURL("image/png");
$("#imageData").val(imgData);
imageData 是隐藏的表单类型,所以我只需设置值。然后将整个表单发布到 PHP 端。我已经检查过并且值传递正确(我对 TwitPic 做了类似的事情,它有效,因为它只接受 toDataURL 值,但 Tumblr 给出了很多问题)。
感谢您的帮助! :)
I'm having some issues trying to get my code to post the HTML canvas into Tumblr as a photo. I'm using PHP, and the code on the server side is as follows:
if(isset($_POST['postphoto']) and $_SESSION['loggedin']) {
$title = $_POST['title'];
$caption = $_POST['caption'];
tags = $_POST['tags'];
$imageData = $_POST['imageData'];
$imageData = substr($imageData,strpos($imageData,",")+1);
$imageData = str_replace(' ','+',$imageData);
# Set access token
$tumblr->set_token($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$data = array();
$data['post'] = array(
'type' => 'photo',
'generator' => 'AppName',
'data' => $imageData,
'name' => $title,
'caption' => $caption,
'tags' => $tags
);
$response = $tumblr->fetch('http://www.tumblr.com/api/write', $data);
if($response['successful']) {
echo "Update successful!<br><br>";
} else {
echo "Update failed. {$response[body]}<br><br>";
}
}
On the JavaScript side, I have the following:
var imgData = canvas.toDataURL("image/png");
$("#imageData").val(imgData);
imageData is of form type hidden so I simply set the value. The entire form is then posted to the PHP side. I have checked and the values are passed correctly (I did a similar thing for TwitPic, it works since it simply takes in the toDataURL value, but Tumblr is giving lots of issue).
Thanks for the help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是因为您正在为照片帖子发布标题?
照片帖子没有标题。让我知道当您删除
'name' => 时它是否有效$标题,
。It could be because you are posting title to photo posts?
Photo posts don't have title. Let me know if it works when u remove
'name' => $title,
.