1

郵便配達員を介して Box API を快適にテストした後、php ファイルから curl リクエストを実行するのに苦労しています。それらのいくつかは実行できますが、 https://developers.box.com/docs/#files-upload-a-fileに記載されているファイル アップロード リクエストを変換する方法がわかりません。上記のリンクで提供されているリクエストの例は次のとおりです。

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 file=@myfile.jpg

私が書いたものは次のとおりです。

<?php
$folder_id = "2934811551"; //fake
$accessToken = "QxYtOeSUCdlu5PwMwxUJSD5BeMP9AaoZ"; //fake
$file_out = "\home\box\upload.json";  //store the JSON response
$file_up = "\home\box\uploadfile.txt"; //file to upload

$curl_url = 'https://upload.box.com/api/2.0/files/content';

$params = array('name' => '@' . $file_up,
                'parent' => array('id' => $folder_id)
               );
$params_json = json_encode($params);


//execute CURL 
$fp = fopen($file_out, "w"); //output file

$ch = curl_init();
$options = array(
         CURLOPT_URL            => $curl_url,           
         CURLOPT_RETURNTRANSFER => true,
         CURLOPT_HTTPHEADER     => array("Authorization: Bearer " . $accessToken),          
         CURLOPT_POST           => true,
         CURLOPT_POSTFIELDS     => $params_json,
         CURLOPT_VERBOSE        => true,
         CURLOPT_FILE           => $fp,     
        );
curl_setopt_array($ch, $options);
$result = curl_exec($ch);

print_r (curl_getinfo($ch));
echo curl_error($ch);

curl_close($ch);

fclose($fp);

?>

結果は次のとおりです。

* About to connect() to upload.box.com port 443 (#0)
*   Trying 74.112.185.182... * connected
* Connected to upload.box.com (74.112.185.182) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_RSA_WITH_AES_256_CBC_SHA
* Server certificate:
*   subject: CN=*.box.com,O="Box, Inc.",L=Los Altos,ST=California,C=US
*   start date: Nov 13 19:22:20 2014 GMT
*   expire date: Oct 23 02:42:03 2017 GMT
*   common name: *.box.com
*   issuer: CN=GeoTrust SSL CA - G4,O=GeoTrust Inc.,C=US
> POST /api/2.0/files/content HTTP/1.1
Host: upload.box.com
Accept: */*
Authorization: Bearer QxYtOeSUCdlu5PwMwxUJSD5BeMP9AaoZ
Content-Length: 55
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 405 Method Not Allowed
< Allow: GET, OPTIONS, HEAD
< Content-Type: text/html;charset=UTF-8
< Content-Length: 0
< Date: Thu, 29 Jan 2015 18:07:49 GMT
< Age: 0
< Connection: keep-alive
< Server: ATS
< 
* Connection #0 to host upload.box.com left intact
Array
(
    [url] => https://upload.box.com/api/2.0/files/content
    [content_type] => text/html;charset=UTF-8
    [http_code] => 405
    [header_size] => 202
    [request_size] => 255
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 5.748647
    [namelookup_time] => 5.007004
    [connect_time] => 5.155813
    [pretransfer_time] => 5.544732
    [size_upload] => 55
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 9
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 5.748583
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)
* Closing connection #0

追加すると、次のようecho $params_json;になります。

{"name":"@\\home\\dianna\\box\\uploadfile.txt","parent":{"id":"2934811551"}}

jkson でエンコードされた文字列の代わりにCURLOPT_POSTFIELDS => $params_json,、配列をCURLOPT_POSTFIELDS => $params,I getで渡しますfailed creating formpost data。この時点で $file_up 変数を編集して、アップロードするファイルの相対 URL を保存すると ($file_up = "uploadfile.txt";このファイルと私の php ファイルは同じフォルダーにあります)、私の php ファイルの出力は次のようになります。

Content-Length: 311
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------650eb8029292

* Done waiting for 100-continue
< HTTP/1.1 100 Continue
< Date: Thu, 29 Jan 2015 18:23:33 GMT
< Server: ATS
< HTTP/1.1 400 Bad Request
< server: ATS
< Date: Thu, 29 Jan 2015 18:23:34 GMT
< Cache-Control: no-cache, no-store
< Content-Type: application/json
< Content-Length: 277
< Age: 2
< Connection: keep-alive
< 
* Connection #0 to host upload.box.com left intact
Array
(
    [url] => https://upload.box.com/api/2.0/files/content
    [content_type] => application/json
    [http_code] => 400
    [header_size] => 273
    [request_size] => 260
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 7.698447
    [namelookup_time] => 5.006963
    [connect_time] => 5.154683
    [pretransfer_time] => 5.540065
    [size_upload] => 311
    [size_download] => 277
    [speed_download] => 35
    [speed_upload] => 40
    [download_content_length] => 277
    [upload_content_length] => 311
    [starttransfer_time] => 6.541224
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)
* Closing connection #0

content-Type は現在 multipart/form-data として設定されていますが、応答はまだ 400 です。このトピックに関するすべての回答と、curl に関する PHP ページのマニュアル ページを読みましたが、これは私が得た限りです。 ... (ところで、PHP 5.3.3 を使用している場合に役立つ場合)

どんな助けでも大歓迎です!

4

3 に答える 3

0

これは、box API を使用して画像をアップロードする方法です。

$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); 
于 2015-03-30T07:23:03.077 に答える
0

これを見てください。私はそれを使用して成功し、box-api と php https://github.com/golchha21/BoxPHPAPIに最適なフレームワークであることがわかりました。

于 2015-02-05T19:21:18.943 に答える