通过 Tumblr API 上传多张图片
我有大约 300 张图片想要上传到我的新 Tumblr 帐户,因为我的旧 WordPress 网站被黑了,我不再希望使用 WordPress。
我连续 300 天每天上传一张图片,我希望能够使用 api 拍摄这些图片并将它们上传到我的 tumblr 网站。
图像当前是本地的,存储在/images/中。它们都有上传日期作为文件名的前十个字符(01-01-2009-filename.png),我也一起发送了这个日期参数。我希望能够通过将 API 的响应输出到我的 error_log 来查看脚本的进度。这是我目前所掌握的基于 tumblr api 页面的内容。
// Authorization info
$tumblr_email = '[email protected]';
$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 也一无所知。
I have about 300 images I want to upload to my new Tumblr account, becuase my old wordpress site got hacked and I no longer wish to use wordpress.
I uploaded one image a day for 300 days, and I'd like to be able to take these images and upload them to my tumblr site using the api.
The images are currently local, stored in /images/. They all have the date they were uploaded as the first ten characters of the filename, (01-01-2009-filename.png) and I went to send this date parameter along as well. I want to be able to see the progress of the script by outputting the responses from the API to my error_log. Here is what I have so far, based on the tumblr api page.
// Authorization info
$tumblr_email = '[email protected]';
$tumblr_password = 'password';
// Tumblr script parameters
$source_directory = "images/";
// For each file, assign the file to a pointer
here's the first stumbling block. How do I get all of the images in the directory and loop through them? Once I have a for or while loop set up I assume this is the next step
$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);
So, I'm stuck on how to use PHP to read a file directory, loop through each of the files, and do things to the name / with the file itself. I also need to know how to set the data parameter, as in choosing multi-part / formdata. I also don't know anything about cURL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
glob
函数快速获取与模式匹配的文件数组。也就是说:要让curl上传文件,您只需向其传递以
@
为前缀的文件名(请参阅http://www.php.net/curl_setopt)。此时你向它传递一个 PHP 文件句柄,这没有多大意义。将$post_data
更改为:你应该没问题。
You can use the
glob
function to quickly get an array of files matching a pattern. That is:To make curl upload the file, you can simply pass it the file name prefixed with an
@
(see theCURLOPT_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:And you should be good.
我用这段代码得到了这个:
I got this working with this code: