简体   繁体   English

cURL到PHP的翻译问题

[英]cURL to PHP Translation Issues

Okay...newbie question that I can't figure out. 好吧...我不知道的新手问题。 Here's what the Box.com documentation says for uploading a file: 这是Box.com文档上载文件的内容:

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

I'm trying this in PHP: 我正在PHP中尝试此操作:

$attributes='{"name":"tigers.jpeg", "parent":{"id":"4224475591"}}';
 $headr = array();
$headr[] = 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxx';

$prep_data = array('file'=>'data08.13.15.csv','attributes' => $attributes);
$post_data=  http_build_query($prep_data) . "\n";
$url = 'https://upload.box.com/api/2.0/files/content';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );
var_dump($response);

I'm getting string(0) "" back for my var_dump($response); 我正在为我的var_dump($response);返回string(0) "" var_dump($response);

What am I doing wrong here? 我在这里做错了什么?

You are not specifying that it is multipart form data. 您未指定它是多部分表单数据。 Not sure if you have other issues as well. 不知道您是否还有其他问题。 But here is some working code you can change to meet your needs: 但是,这里有一些工作代码可以更改,以满足您的需求:

<?php
    // ENTER YOUR DEVELOPER TOKEN
    $token = "ekdfokeEdfdfkosdkoqwekof93kofsdfkosodSqd";

    $url = "https://upload.box.com/api/2.0/files/content";
    if (isset($_POST['btnUpload'])) {
        $file_upload = $_FILES['file']['tmp_name'];
        $json = json_encode(array(
                                'name' => $_FILES['file']['name'], 
                                'parent' => array('id' => 0)
                            ));
        $fields = array(
                      'attributes' => $json,
                      'file'=>new CurlFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name'])
                  );

        try {
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer '.$token, 
                'Content-Type:multipart/form-data'
            ));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $response = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $response = $e->getMessage();
        }

        print_r($response);
    }

?>

<form method="post" name="frmUpload" enctype="multipart/form-data">
    <label>Upload file to Box
        <input name="file" type="file" id="file"/>
    </label>
    <input name="btnUpload" type="submit" value="Upload" />
</form>

http://liljosh.com/uploading-files-to-box-content-api-v2/ http://liljosh.com/uploading-files-to-box-content-api-v2/

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

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