2017-10-12 16 views
2

ビデオをアップロードしてリモートの宛先に送信できる形式があります。私はGuzzleを使用してPHPに '翻訳'したいcURLリクエストを持っています。Guzzleを使用してファイルをアップロード

public function upload(Request $request) 
    { 
     $file  = $request->file('file'); 
     $fileName = $file->getClientOriginalName(); 
     $realPath = $file->getRealPath(); 

     $client = new Client(); 
     $response = $client->request('POST', 'http://mydomain.de:8080/spots', [ 
      'multipart' => [ 
       [ 
        'name'  => 'spotid', 
        'country' => 'DE', 
        'contents' => file_get_contents($realPath), 
       ], 
       [ 
        'type' => 'video/mp4', 
       ], 
      ], 
     ]); 

     dd($response); 

    } 

これは私が使用してPHPに変換したいのcURLです::だから私は動画をアップロードする際に

curl -X POST -F 'body={"name":"Test","country":"Deutschland"};type=application/json' -F '[email protected]:\Users\PROD\Downloads\617103.mp4;type= video/mp4 ' http://mydomain.de:8080/spots 

を、私はこのハードコードさを置き換えたい

は、これまでのところ私はこれを持っています

C:¥Users¥PROD¥Downloads¥617103.mp4

私はこれを実行すると、私はエラーを取得:

Client error: POST http://mydomain.de:8080/spots resulted in a 400 Bad Request response: request body invalid: expecting form value 'body`'

Client error: POST http://mydomain.de/spots resulted in a 400 Bad Request response: request body invalid: expecting form value 'body'

答えて

2

私はがつがつ食うのmultipart要求オプションを確認したいです。

  1. JSONデータを使用して、カールリクエスト(それが紛らわしいbodyという名前です)で使用している同じ名前を使って文字列に変換し、合格する必要があります:私は2つの問題を参照してください。
  2. curl要求内のtypeは、ヘッダーContent-Typeにマップされます。 $ man curlから:

    $response = $client->request('POST', 'http://mydomain.de:8080/spots', [ 
        'multipart' => [ 
         [ 
          'name'  => 'body', 
          'contents' => json_encode(['name' => 'Test', 'country' => 'Deutschland']), 
          'headers' => ['Content-Type' => 'application/json'] 
         ], 
         [ 
          'name'  => 'file', 
          'contents' => fopen('617103.mp4', 'r'), 
          'headers' => ['Content-Type' => 'video/mp4'] 
         ], 
        ], 
    ]); 
    

    You can also tell curl what Content-Type to use by using 'type='.

のようなものを試してみてください

関連する問題