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 来读取文件目录,循环遍历每个文件,并使用文件本身对名称 / 做一些事情。我还需要知道如何设置数据参数,如选择多部分/表单数据。我对 cURL 也一无所知。

4

2 回答 2

1

You can use the glob function to quickly get an array of files matching a pattern. That is:

foreach (glob('images/*.png') as $current_image) {
  ...
}

To make curl upload the file, you can simply pass it the file name prefixed with an @ (see the CURLOPT_POSTFIELDS description at http://www.php.net/curl_setopt). At the minute you're passing it a PHP file handle, which doesn't make too much sense. Change $post_data to:

$post_data = '@' . dirname(__FILE__) . '/' . $current_image;

And you should be good.

于 2010-05-25T10:36:09.387 回答
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 :(";

}
?>
于 2012-07-25T00:34:34.580 回答