doutao5419 2010-10-03 20:20
浏览 51
已采纳

使用php / curl将图像上传到tumblr时出现问题

I've been trying to create a simple php file that will upload a single image to tumblr. An example is listed here, but it just won't work with images. Here is the code:

<?php
// Authorization info
$tumblr_email    = 'lol@something.com';
$tumblr_password = 'secret';

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

//////////////////////THIS DOESN'T WORK/////////////////////////////////
//$filename = "/Applications/MAMP/htdocs/1.jpeg";
//$handle = fopen($filename, "r");
//$post_data = fread($handle, filesize($filename));

//////////////////////NEITHER DOES THIS/////////////////////////////////
//$post_data = "/Applications/MAMP/htdocs/1.jpg";

///////////////////////////////NOR THIS/////////////////////////////////
//$filename = "/Applications/MAMP/htdocs/1.jpeg";
//$post_data = fopen($filename, "r");


// Prepare POST request
$request_data = http_build_query(
    array(
        'email'     => $tumblr_email,
        'password'  => $tumblr_password,
        'type'      => $post_type,
        'data'      => $post_data,
        'generator' => 'testing tumblr API example'
    )
);

// 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.
";
} else if ($status == 403) {
    echo 'Bad email or password';
} else {
    echo "Error: $result
";
}

?> 

I am not that familiar with php/curl, so I have no idea what am I doing wrong. This is also written on tumblr.com:

File uploads can be done in a data parameter where specified above. You may use either of the common encoding methods:

1) multipart/form-data method, like a file upload box in a web form. Maximum size:

...10 MB for photos...

This is recommended since there's much less overhead.

2) Normal POST method, in which the file's entire binary contents are URL-encoded like any other POST variable. Maximum size:

...5 MB for photos...

Thanks.

  • 写回答

1条回答 默认 最新

  • douyujun0152 2010-10-05 01:30
    关注

    Try this:

    // Authorization info
    $tumblr_email    = 'lol@something.com';
    $tumblr_password = 'secret';
    
    // Data for new record
    $post_type  = 'photo';
    
    $post_data = "/Applications/MAMP/htdocs/1.jpg";
    
    $request_data = array(
            'email'     => $tumblr_email,
            'password'  => $tumblr_password,
            'type'      => $post_type,
            'data'      => '@'.$post_data,
            'generator' => 'testing tumblr API example'
    );
    
    // 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);
    

    You need to pass an array to CURLOPT_POSTFIELDS, and prepend the absolute path to the file with an @. This will make cURL post the data as multipart/form-data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同