简体   繁体   English

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

[英]curl_exec for uploading files in box api using php fails

I am able to upload the file using curl on the terminal with the following: 我可以使用以下命令在终端上使用curl上传文件:

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: 但是,当我尝试通过以下代码使用PHP执行相同操作时:

$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'. 无论我做什么,content_type都保留为“ 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. 设置CURLOPT_VERBOSE或检查错误也不返回任何内容。 While without the CURLOPT_RETURNTRANSFER option, curl_exec returns true. 如果没有CURLOPT_RETURNTRANSFER选项,curl_exec将返回true。 Can someone help me understand where I am making a mistake here? 有人可以帮助我了解我在这里犯错的地方吗?

I was not able to solve this problem using php itself but using shell_exec() to run the fully form curl command actually worked. 我无法使用php本身解决此问题,但使用shell_exec()来运行完全有效的curl命令。 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. 希望对您有所帮助。

http_code=400 Bad Request. http_code = 400错误的请求。

Try to set your header enctype='multipart/form-data',as upload file use form. 尝试将您的标头enctype ='multipart / form-data'设置为上传文件使用表格。

CURLOPT_POST TRUE to do a regular HTTP POST. CURLOPT_POST为TRUE可进行常规HTTP POST。 This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms. 该POST是正常的application / x-www-form-urlencoded类型,最常用于HTML表单。

more php.net 更多php.net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM