2011-07-15 22 views
3

我試圖使用圖形API批量請求上傳圖像,但我無法上傳使用內嵌圖像,任何人都可以幫助我嗎?如何使用圖形API批量請求上傳內嵌圖像?

批量請求文件:https://developers.facebook.com/docs/reference/api/batch/

照片批量上傳:http://developers.facebook.com/blog/post/493/

照片批量上傳博客文章代碼工作正常,但我想從我的服務器上傳圖片,而不是從我的電腦,例如:/的public_html /image/pic.jpg,我不知道我該怎麼做。

編輯:我使用PHP SDK 3.0.1

這裏是我的代碼:

<?php 
    CODE WAS CHANGED AND THE PROBLEM IS FIXED ALREADY, SEE THE ANSWER BELOW 
?> 

這是他們的文件:

上傳二進制數據

二進制數據可以指定爲的multipart/mime部分的一部分批處理API請求。批量圖形API允許上傳多個 二進制項目作爲批處理調用的一部分。爲此,您需要 將所有二進制項目作爲多部分/ MIME附件添加到您的 請求中,並且需要每個操作在操作中使用 屬性「attached_files」引用其二進制項目。 「attached_files」 屬性可以在其 值中使用逗號分隔的附件名稱列表。

下面的例子演示瞭如何上傳2張照片中,一次 電話:

curl 
    –F ‘access_token=…’ \ 
    -F ‘batch=[{「method」:」POST」, \ 
        「relative_url」:」me/photos」, \ 
        「body」:」message=My cat photo」 \ 
        "attached_files":"file1" \ 
       }, 
       {「method」:」POST」, \ 
        「relative_url」:」me/photos」, \ 
        「body」:」message=My dog photo」 \ 
        "attached_files":"file2" \ 
       }, 
       ]’ 
    -F ‘[email protected]’ \ 
    -F '[email protected]' \ 
    https://graph.facebook.com 

編輯2:

回答

8

第一個問題,我看到的是,該批不應該是URL的一部分,但該則params的一部分,而...

請參見下面的粗批次例如:

$batch = array(); 

$req = array(
    'method'  => 'GET', 
    'relative_url' => '/me' 
); 

$batch[] = json_encode($req); 

$req = array(
    'method'  => 'GET', 
    'relative_url' => '/me/albums' 
); 

$batch[] = json_encode($req); 

$params = array(
    'batch' => '[' . implode(',',$batch) . ']' 
); 
try { 
    $info = $facebook->api('/','POST',$params); 
} catch(FacebookApiException $e) { 
    error_log($e); 
    $info = null; 
} 
if(!empty($info)){ 
    if($info[0]['code'] == '200'){ 
     $user_profile = json_decode($info[0]['body']); 
    } 
    if($info[1]['code'] == '200'){ 
     $user_albums = json_decode($info[1]['body']); 
    } 
    echo "<pre>User Profile:\n"; 
    print_r($user_profile); 
    echo "\nAlbums\n"; 
    print_r($user_albums); 
    echo "<pre>"; 
} 

通知特別是$ facebook-> API調用是如何格式化...

編輯:

這裏是一個工作批量圖片上傳:

$files = array(
    '/data/Pictures/pic1.jpg', 
    '/data/Pictures/pic2.jpg', 
    '/data/Pictures/pic3.jpg' 
); 

//Must set upload support to true 
$facebook->setFileUploadSupport(true); 

$batch  = array(); 
$params = array(); 
$count = 1; 
foreach($files as $file){ 
    $req = array(
     'method'   => 'POST', 
     'relative_url' => '/me/photos', 
     'attached_files' => 'file' . $count 
    ); 
    //add this request to the batch ... 
    $batch[] = json_encode($req); 

    //set the filepath for the file to be uploaded 
    $params['file'.$count] = '@' . realpath($file); 

    $count++; 
} 
$params['batch'] = '[' . implode(',',$batch) . ']'; 

try{ 
    $upload = $facebook->api('/','post',$params); 
} catch(FacebookApiException $e) { 
    error_log($e); 
    $upload = null; 
} 

//View the results ... 
if(!is_null($upload)){ 
    echo "<pre>" . print_r($upload,1) . "<pre>"; 
    echo "<hr />"; 
} 

菊ST測試,它的工作原理就像一個魅力...

+0

真棒男人,最後它的作品:)謝謝隊友。 –

+0

好,很清楚,謝謝! – Aston

+0

@dleiftah你好先生,我一直在嘗試你的解決方案,我似乎並沒有得到它的工作。它給我(#324)需要上傳文件。 –

0

嗯,我也不太清楚,我不能籤目前,但

http://au.php.net/manual/en/function.curl-setopt.php

查看它在CURLOPT_POSTFIELDS,它說:

要在HTTP「POST」操作中發佈的完整數據。 要發佈文件, 需要在文件名前添加@並使用完整路徑。文件類型可以是 ,其格式爲: format'; type = mimetype'。此參數可以作爲 urlencoded字符串傳遞,如'para1 = val1 & para2 = val2 & ...'或作爲數組與 字段名稱作爲鍵和字段數據作爲值。如果value是一個數組,則 的Content-Type標題將被設置爲multipart/form-data。從PHP 5.2.0開始,使用@前綴傳遞給此選項的文件必須以數組形式工作。

這裏是另一個捲曲例如:

CURL PHP send image

所以你需要做的是

$queries = array(
    array("method" => "POST", "relative_url" => "me/photos","body" => "message=cool","attached_files" => 'file1') 
); 

$batch = $facebook->api("/?batch=".json_encode($queries)."&[email protected]", 'POST'); 
+0

不,它不工作:(嘗試一些其他的東西爲好,但沒有運氣:S –

0
// // File you want to upload/post 
$post_data['file1'] = "@D:/home/2.jpg"; 
$post_data['file2'] = "@D:/home/1.jpg"; 

// Initialize cURL 
$ch = curl_init(); 
// Set URL on which you want to post the Form and/or data 
curl_setopt($ch, CURLOPT_URL, $post_url); 
// Data+Files to be posted 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
// Pass TRUE or 1 if you want to wait for and catch the response against the request made 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
// For Debug mode; shows up any error encountered during the operation 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
// Execute the request 
$response = curl_exec($ch); 
// echo curl_errno($ch); 
// echo curl_error($ch); 
// Just for debug: to see response 
echo $response; 

這將肯定其工作爲我工作

相關問題