2017-10-12 2 views
2

Ich habe ein Formular, wo Video hochgeladen und an Remote-Ziel gesendet werden kann. Ich habe eine cURL-Anfrage, die ich mit Guzzle nach PHP übersetzen möchte.Hochladen von Datei mit Guzzle

Bisher habe ich dies:

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); 

    } 

Diese cURL ist, die ich verwenden und wollen PHP übersetzen:

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 

Also, wenn ich das Video hochladen, ich möchte diese Hardcoded ersetzen

C: \ Benutzer \ PROD \ Downloads \ 617103.mp4.

Wenn ich dies ausführen, bekomme ich einen Fehler:

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'

Antwort

2

Ich würde die multipart Anfrage Optionen des Guzzle überprüfen. Ich sehe zwei Probleme:

  1. Die JSON-Daten muss mit dem gleichen Namen und Zeichenfolge weitergegeben werden Sie in der curl Anfrage verwenden (es ist body zum Verwechseln genannt).
  2. Die type in der Curl-Anforderung entspricht der Kopfzeile . Von $ man curl:

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

Versuchen Sie so etwas wie:

$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'] 
     ], 
    ], 
]);