在Box V2 API中苦苦挣扎,通过PHP上传文件

时间:2015-01-29 18:38:27

标签: php curl box boxapiv2

在通过邮递员感觉舒适测试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"}}

如果不是CURLOPT_POSTFIELDS => $params_json,处的jkson编码字符串,而是使用CURLOPT_POSTFIELDS => $params,传递数组,我会得到failed creating formpost data。如果此时我编辑$ file_up var来存储要上传的文件的相对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)

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

看看这个。我已经成功使用它并发现它是box-api和php的最佳框架https://github.com/golchha21/BoxPHPAPI

答案 1 :(得分:0)

这是我使用方框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); 

答案 2 :(得分:0)

上周我花了很多时间在同一个问题上挣扎。这里发布的另外两个答案对我们不起作用。我也找不到任何与PHP相关的建议。最后决定调用运行curl的外部unix脚本:

#!/bin/bash
#upload.sh
ACCESS_TOKEN="$1"
FOLDER_ID="$2"
FILE="$3"
HEADER="Authorization: Bearer $ACCESS_TOKEN"

STATUS=`curl "https://upload.box.com/api/2.0/files/content" -H "$HEADER" -F parent_id=${FOLDER_ID} -F filename=@${FILE} -i -s | grep HTTP/1.1 | tail -1 | awk {'print $2'}` > /dev/null

if [ $STATUS == 201 ]
then
    #OK
else
    #Upload failed
fi

PHP中的调用格式为:

$result = shell_exec("upload.sh $box_token $folder_id $file_name");

不满意,但确实有效。只有建议我可以考虑为什么PHP CURL不起作用是管道中的东西弄乱了令牌。这是在PHP 5.5.15。