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"}}

CURLOPT_POSTFIELDS => $params_json,如果在我传递数组而不是 jkson 编码的字符串时,CURLOPT_POSTFIELDS => $params,我得到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 回答
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 上。

于 2015-04-19T22:54:15.780 回答