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