2011-03-25 85 views
3

也許我會發瘋,但這是行不通的。我正嘗試將連接文件夾中的圖像上傳到tumblr帳戶。這裏是我的代碼:Tumblr API上傳

<?php 

// Authorization info 
$tumblr_email = '***'; 
$tumblr_password = '***'; 

// 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'    => $post_type, 
    'data'    => 'connect/19.jpg', 
    'generator'   => 'testing' 
) 
); 

// 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 ($status): $result\n"; 
} 

?> 

<img src="connect/19.jpg" /> 

我收到此錯誤:

+0

你得到從API的結果是什麼? – 2011-03-25 23:32:40

+0

我只收到錯誤,錯誤(400):錯誤上傳照片。 – iosfreak 2011-03-25 23:33:54

回答

2

這就像要發送到的圖像,而不是圖像本身的路徑在我看來。
如何與

'data'    => file_get_contents('connect/19.jpg'),

1

更換

'data'    => 'connect/19.jpg',
只需添加如下代碼:

// Prepare POST request 

$request_data = http_build_query(

array(

    'email'    => $tumblr_email, 
    'password'   => $tumblr_password, 
    'type'    => $post_type, 
    'caption'   => $post_title, 
    'data'    => file_get_contents('http://27.media.tumblr.com/tumblr_lywood4slh1qz7t0xo1_250.jpg'), 
    'generator'   => 'testing' 
) 
);