Tumblr API 上传

3
也许我有点疯了,但是这行代码不管用。我想将一个图片上传到Tumblr账户的connect文件夹中。以下是我的代码:
<?php

// Authorization info
$tumblr_email    = '***';
$tumblr_password = '***';

// Data for new record
$post_type  = 'photo';
$post_title = 'The post title';
$post_body  = 'This is the body of the post.';

// Prepare POST request
$request_data = http_build_query(
array(
    'email'             => $tumblr_email,
    'password'          => $tumblr_password,
    'type'              => $post_type,
    'data'              => 'connect/19.jpg',
    'generator'         => 'testing'
)
);

// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error ($status): $result\n";
}

?>

<img src="connect/19.jpg" />

我遇到了这个错误:400


你从 API 中得到什么结果? - Teo Klestrup Röijezon
我只收到一个错误,错误(400):上传照片出错。 - iosfreak
2个回答

2

我认为你发送的是图片路径而不是图片本身。

'data'              => 'connect/19.jpg',
替换为
'data'              => file_get_contents('connect/19.jpg'),
可以解决问题。


1

只需添加以下代码:

// Prepare POST request

$request_data = http_build_query(

array(

    'email'             => $tumblr_email,
    'password'          => $tumblr_password,
    'type'              => $post_type,
    'caption'           => $post_title,
    'data'              => file_get_contents('http://27.media.tumblr.com/tumblr_lywood4slh1qz7t0xo1_250.jpg'),
    'generator'         => 'testing'
)
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接