首页 文章

通过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来读取文件目录,遍历每个文件,并使用文件本身对名称进行操作 . 我还需要知道如何设置数据参数,就像选择multi-part / formdata一样 . 我也对cURL一无所知 .

2 回答

  • 1

    我得到了这个代码:

    <?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 :(";
    
    }
    ?>
    
  • 0

    您可以使用 glob 函数快速获取与模式匹配的文件数组 . 那是:

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

    要使curl上传文件,您只需传递前缀为 @ 的文件名(请参阅http://www.php.net/curl_setopt处的 CURLOPT_POSTFIELDS 说明) . 在那一刻你没有多大意义 . 将 $post_data 更改为:

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

    你应该好 .

相关问题