2012-11-14 37 views
5

在此頁面上傳文件:http://developers.box.com/docs/我如何使用PHP

使用cURL上傳文件:

METHOD 
POST /files/content 
EXAMPLE REQUEST 
curl https://api.box.com/2.0/files/content \ 
-H "Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN" \ 
-F [email protected]_NAME1 \ 
-F [email protected]_NAME2 \ 
-F folder_id=FOLDER_ID 

,但現在,我想用PHP上傳文件,我怎麼能做到這一點?我的代碼:

<?php  
$params = array(); 
$params['folder_id'] = '485272014'; 

$u_file = fopen("D:\code\php\bcs\test.data", "r"); 

$params['filename1'] = $u_file; 

$params = json_encode($params); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/content"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 

curl_setopt($ch, CURLOPT_UPLOAD, true); 



curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key=API_KEY&auth_token=TOKEN")); 
$result = curl_exec($ch); 
curl_close($ch); 
print_r($result); 

fclose($u_file); 

?> 

它沒有工作,我運行使用腳本:php -f test.php

+0

嘗試直接'$ PARAMS [ '文件名1'] =「@D:\ code \ php \ bcs \ test.data」;' – GBD

回答

6
  1. 我不認爲POST表單數據可以接受文件處理程序使用
    fopen("D:\code\php\bcs\test.data", "r");

    創建

    嘗試通過使用@來訪問文件處理程序。順便說一句,更改\爲/所以你不小心把一些字符作爲轉義字符:
    $u_file = "@D:/code/php/bcs/test.data";

  2. 你不應該json_encode的內容,如果你的文件內容不是文本(說,圖像/二進制文件)。

  3. 我覺得這一行也會給你帶來問題。試用了這個選項我的代碼,給我一個奇怪的「441所需長度」的錯誤。我的代碼工作正常,沒有這個選項:
    curl_setopt($ch, CURLOPT_UPLOAD, true);

最後,這裏是我的工作代碼:

<?php 
public function upload_file() 
{ 
    $url = 'https://api.box.com/2.0/files/content'; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_ENCODING, "UTF-8"); 

    //this is my method to construct the Authorisation header 
    $header_details = array($this->default_authen_header()); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_details); 

    $post_vars = array(); 
    $post_vars['filename'] = "@C:/tmp_touch.txt"; 
    $post_vars['folder_id'] = 0; 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars); 
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
?> 
5
<?php 
    // ENTER YOUR DEVELOPER TOKEN 
    $token = "ekdfokeEdfdfkosdkoqwekof93kofsdfkosodSqd"; 

    $url = "https://upload.box.com/api/2.0/files/content"; 
    if (isset($_POST['btnUpload'])) { 
     $file_upload = $_FILES['file']['tmp_name']; 
     $json = json_encode(array(
           'name' => $_FILES['file']['name'], 
           'parent' => array('id' => 0) 
          )); 
     $fields = array(
         'attributes' => $json, 
         'file'=>new CurlFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']) 
       ); 

     try { 
      $ch = curl_init(); 
      curl_setopt($ch,CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       'Authorization: Bearer '.$token, 
       'Content-Type:multipart/form-data' 
      )); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
      $response = curl_exec($ch); 
      curl_close($ch); 
     } catch (Exception $e) { 
      $response = $e->getMessage(); 
     } 

     print_r($response); 
    } 

?> 

<form method="post" name="frmUpload" enctype="multipart/form-data"> 
    <tr> 
     <td>Upload</td> 
     <td align="center">:</td> 
     <td><input name="file" type="file" id="file"/></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td align="center">&nbsp;</td> 
     <td><input name="btnUpload" type="submit" value="Upload" /></td> 
    </tr> 
</form> 

http://liljosh.com/uploading-files-to-box-content-api-v2/