douyalin2258 2015-06-18 03:44
浏览 74
已采纳

使用php在box api上传文件的curl_exec失败

I am able to upload the file using curl on the terminal with the following:

curl https://upload.box.com/api/2.0/files/content \
> -H "Authorization: Bearer {access-code}" -X POST \
> -F attributes='{"name":"tested.png", "parent":{"id":"3804480350"}}' \
> -F file=@/Applications/MAMP/htdocs/BoxappTest/test.png

But when I try to do the same using PHP with the following code:

$filePath=realPath("./test.png");
$xmlfile = file_get_contents("codes.xml");          
$codes_data = simplexml_load_string($xmlfile);
$access_token=$codes_data->access_code;
$destination_filename="tested.png";
$id="3804480350";
   $ch=  curl_init();    
   curl_reset($ch);
echo "Uploading file...",'<br>';
$post = '{"name":"'.$destination_filename.'", "parent":{"id":"'.$id.'"}}';
$postfields = Array("attributes"=>$post,"file" => "@".$filePath);        
$headers= Array("Authorization: Bearer " . $access_token);

    curl_setopt($ch, CURLOPT_URL, "https://upload.box.com/api/2.0/files/content");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $output=curl_exec($ch);
    $info=curl_getinfo($ch);
    curl_close($ch);

    if(curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch); 
    }

    foreach($info as $data => $part) {
        echo $data."=".$part, '<br>';
    }

    echo "File uploaded...",'<br>';
    var_dump($output);

It gives me the following output:

Uploading file...
content_type=text/html;charset=UTF-8
http_code=400
header_size=243
request_size=260
filetime=-1
ssl_verify_result=0
redirect_count=0
total_time=1.118914
namelookup_time=0.000738
connect_time=0.022878
pretransfer_time=0.100239
size_upload=335
size_download=0
speed_download=0
speed_upload=299
download_content_length=0
upload_content_length=335
starttransfer_time=1.10153
redirect_time=0
redirect_url=
primary_ip=74.112.185.182
certinfo=Array
primary_port=443
local_ip=10.0.0.188
local_port=50008
request_header=POST /api/2.0/files/content HTTP/1.1 Host: upload.box.com Accept: */* Authorization: Bearer {access-code} Content-Length: 335 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------....
File uploaded...
string(0) ""

I have gone over various sites but none of them seem to be right for my problem. No matter what I do, the content_type stays 'text/html'. I have tried to set it in the header and the post fields. But both didn't work. I know I am getting the access codes properly and that they are valid. I am not sure about the curl options though. Setting CURLOPT_VERBOSE or checking for errors does not return anything either. While without the CURLOPT_RETURNTRANSFER option, curl_exec returns true. Can someone help me understand where I am making a mistake here?

  • 写回答

2条回答 默认 最新

  • dongzaotiao2863 2015-07-24 01:55
    关注

    I was not able to solve this problem using php itself but using shell_exec() to run the fully form curl command actually worked. So I have stuck to that implementation.

        $cmd = "curl https://upload.box.com/api/2.0/files/content \
                -H \"Authorization: Bearer $access_token\" -X POST \
                -F attributes='{\"name\":\"$dest_name\",\"parent\": {\"id\":\"$parent_id\"}}' \
                -F file=@\"$filePath\"";
    
        $result=shell_exec($cmd);
    
        return result;
    

    I hope this helps somebody.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题