dts777777 2015-11-14 22:00
浏览 14
已采纳

如何使用laravel向box.com API发送帖子请求?

I'm trying to make a post request from my controller to the Box.com API. The request will upload a file to my Box.com account. The following code is on my view:

<form action="/accept" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}

    <input type="file" name="filename">
    <input type="text" name="parent_id" value="0">

    <button type="submit">Submit</button>
</form>

In my controller, I'm not getting any clue of how can I send the post request to the box.com API which will send the form data with an authorization header. The Box.com API require the authorization header to accept the request.

However, in Laravel documentation they've a section where I found the following code:

return response($content)
        ->header('Content-Type', application/json)
        ->header('Authorization', 'Bearer XXXXXXXXXXXXXXXX');

I can set the authorization header using this however, how the form data would be send then?

Here's the Box.com API URL to accept the post request:

https://upload.box.com/api/2.0/files/content
  • 写回答

2条回答 默认 最新

  • dongtui9168 2015-11-15 04:38
    关注

    I definitely agree with pespantelis, using a PHP HTTP Client like Guzzle is a great way to go with what you wish to do.

    Once you're all set up it is just a matter of formatting your request, e.g you could do what you're trying to do like so:

    Route::get( '/', function ()
    {
        $client = new GuzzleHttp\Client( ["base_uri" => "https://upload.box.com/api/2.0/"] );
    
        $response = $client->request( "GET", "files/content", ["headers" => ["Authorization" => "Bearer XXXXXXXXXXXXXXXX", "Content-Type" => "application/json"]] );
    
        return Response::json($response);
    } );
    

    Although I would consider using the Official SDK or the Community-Built SDK as pespantelis suggests as that may be easier to grasp since they lay it all out for you.

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

报告相同问题?