php
  • curl
  • 2015-08-14 98 views 2 likes 
    2

    好吧...我無法弄清楚的新手問題。下面介紹一下Box.com documentation說,對上傳文件:cURL到PHP的翻譯問題

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

    我在PHP嘗試此:

    $attributes='{"name":"tigers.jpeg", "parent":{"id":"4224475591"}}'; 
    $headr = array(); 
    $headr[] = 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
    
    $prep_data = array('file'=>'data08.13.15.csv','attributes' => $attributes); 
    $post_data= http_build_query($prep_data) . "\n"; 
    $url = 'https://upload.box.com/api/2.0/files/content'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headr); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    
    $response = curl_exec($ch); 
    var_dump($response); 
    

    我得到string(0) ""回我var_dump($response);

    我在做什麼錯這裏?

    回答

    0

    您沒有指定它是多部分表單數據。不知道你是否還有其他問題。但這裏是一些工作的代碼,你可以改變,以滿足您的需求:

    <?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"> 
        <label>Upload file to Box 
         <input name="file" type="file" id="file"/> 
        </label> 
        <input name="btnUpload" type="submit" value="Upload" /> 
    </form> 
    

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

    相關問題