0

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
  • did you ever get this working?, I've got the same problem using curl. Commented Jul 22, 2015 at 22:58
  • not really, no. I used shell_exec() to run the curl request. that worked. Commented Jul 24, 2015 at 1:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

http_code=400 Bad Request.

Try to set your header enctype='multipart/form-data',as upload file use form.

CURLOPT_POST TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

more php.net

3 Comments

Technically speaking, php's curl should make it multipart all by itself if there are any postfields, right?
you should tell him first.Do you try?
According to this : php.net/curl_setopt, If value is an array, the Content-Type header will be set to multipart/form-data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.