通过Tumblr API上传多张图片

5
我有大约300张图片想要上传到我的新Tumblr账号,因为我的旧的WordPress网站被黑了,我不再希望使用WordPress。
我每天上传一张图片,共上传了300天,我希望能够使用API将这些图片上传到我的Tumblr网站上。
这些图片目前存储在本地的/images/文件夹中。它们的文件名的前十个字符是上传日期(例如01-01-2009-filename.png),我也想将这个日期参数一起发送。我想通过将API的响应输出到我的error_log来查看脚本的进度。根据Tumblr API页面,以下是我目前拥有的代码。
// Authorization info
$tumblr_email    = 'me@example.com';
$tumblr_password = 'password';

// Tumblr script parameters
$source_directory = "images/";

// For each file, assign the file to a pointer

这是第一个难点。我该如何获取目录中的所有图像并循环遍历它们?一旦我设置了for或while循环,我认为这就是下一步。

$post_data = fopen(dir(__FILE__) . $source_directory . $current_image, 'r');
$post_date = substr($current_image, 0, 10);


// Data for new record
$post_type  = 'photo';

// Prepare POST request
$request_data = http_build_query(
    array(
        'email' => $tumblr_email,
        'password' => $tumblr_password,
        'type' => $post_type,
        'data' => $post_data,
        'date' => $post_date,
        'generator' => 'Multi-file uploader'
    )
);

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

// Output response to error_log
error_log($result);

我卡在如何使用PHP读取文件目录、循环遍历每个文件并对名称/文件本身进行操作上。我还需要知道如何设置数据参数,例如选择多部分/formdata。我也不了解cURL。

2个回答

1
你可以使用 glob 函数快速获取与模式匹配的文件数组。例如:
foreach (glob('images/*.png') as $current_image) {
  ...
}

为了让curl上传文件,您可以简单地将文件名加上前缀@传递给它(请参见http://www.php.net/curl_setopt中的CURLOPT_POSTFIELDS描述)。目前您正在传递一个PHP文件句柄,这没有太多意义。将$post_data更改为:
$post_data = '@' . dirname(__FILE__) . '/' . $current_image;

你应该没问题了。


0

我用这段代码使它工作了:

<?php
// Authorization info
$tumblr_email    = 'email';
$tumblr_password = 'password';
$tumblr_url = 'yourtumblr.tumblr.com';

$directory = getcwd();
$images = glob("./{*.jpeg,*.gif,*.png,*jpg}", GLOB_BRACE);
if ($images) {
foreach($images as $image) {

$post_data = $directory."/".$image;

// 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'      => 'photo',
        'state'     => 'queue',
        'data'      => file_get_contents($post_data),
        'group'     => $tumblr_url
    )
);

// 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: $result\n";
}

}

} else {

echo "No images in folder :(";

}
?>

这段代码已经失效了。我建议使用https://gist.github.com/velocityzen/1242662。 - westondeboer

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