php
  • curl
  • box-api
  • boxapiv2
  • 2014-12-02 4 views 4 likes 
    4

    Box API를 사용하여 box.com에 파일을 업로드하려고합니다.Box.com PHP API로 파일 업로드

    curl https://upload.box.com/api/2.0/files/content \ 
        -H "Authorization: Bearer ACCESS_TOKEN" -X POST \ 
        -F attributes='{"name":nameOftheFile, "parent":{"id":parentId}}' \ 
        -F [email protected] 
    

    가 여기에 내가했던 일이야 : :

    $token = "......"; 
    $url = https://upload.box.com/api/2.0/files/content; 
    $file_upload; 
    
    foreach ($_FILES['file']['name'] as $position => $file) { 
        $file_upload = $_FILES['file']['tmp_name'][$position]; 
    } 
    $json = json_encode(array('name' => $file ,array('parent' => array('id' => 0)))); 
    $attrs = array('attributes' => $json,'file'=>'@'.$file_upload); 
    
    $this->post($url,($attrs)); 
    
    // Post function 
    function post($url,$fields){ 
        try {  
         $ch = curl_init();   
         curl_setopt($ch,CURLOPT_URL, $url); 
         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Authorization: Bearer '.$this->token 
         )); 
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
         self::$response = curl_exec($ch); 
         curl_close($ch); 
    
        } catch (Exception $e) { 
         self::$response = $e->getMessage(); 
        }  
    
        return self::$response; 
    } 
    

    을하지만 그것은 작동하지 않습니다 문서에 따르면, 컬 요청은 다음과 같이한다. 컬 부분에 이상이 있습니까?

    +0

    당신이 볼, 적어도 ''파일 '=> "@의 $의 file_upload"를 필요로'과 PHP 배열 대신 JSON 입력을 제공합니다 : http://stackoverflow.com/questions/21905942/게시 - raw-image-data-as-multipart-form-data-in-file 부분에 대한 데이터 –

    +0

    @HansZandbelt json_encode ($ attrs)를 사용하므로 업로드하지 않은 json – Prakhar

    +0

    으로 변환됩니다. 파일. 당신은 파일 이름을 몇 개의 json에 채우고 그것을 보내고 있습니다. 그리고'[ 'file']'이름으로 다중 파일 업로드를 허용한다면, 코드는 마지막 파일 만 업로드 할 것입니다. 또한 업로드가 성공했다고 가정합니다. –

    답변

    3

    '@path'대신 CurlFile을 사용하면 문제가 해결됩니다!

    -1
    $attributes = array('name'=>time().'.jpg','parent'=>array('id'=>$parent_id)); 
    
    $params = array('attributes' => json_encode($attributes), 'file' => "@".realpath($filename)); $headers = array("Authorization: Bearer ".$accesstoken); 
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    
    curl_setopt($ch, CURLOPT_POST, true); 
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    
    $data = curl_exec($ch); 
    
    curl_close($ch); 
    
    3
    <?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/

    관련 문제