【问题标题】:Tumblr API UploadTumblr API 上传
【发布时间】:2011-03-25 23:27:17
【问题描述】:

也许我快疯了,但这不起作用。我正在尝试将连接文件夹中的图像上传到 tumblr 帐户。这是我的代码:

<?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 中得到什么结果?
  • 我只得到一个错误,Error (400): Error uploading photo.

标签: php api tumblr


【解决方案1】:

在我看来,您发送的是图像的路径,而不是图像本身。
换一个怎么样

'data' => 'connect/19.jpg',
with
'data' => file_get_contents('connect/19.jpg'),

【讨论】:

    【解决方案2】:

    只需添加以下代码:

    // 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'
    )
    );
    

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 2011-02-21
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多