5

Rest 経由でファイルをアップロードし、いくつかの構成も送信する必要があります。

これが私のコード例です:

$this->login();
$files = array('file'=>'aTest1.jpg');
$data =
    array(
        'name'=>'first file',
        'description'=>'first file description',
        'author'=>'test user'
    );
$response = Request::post($this->getRoute('test'))
    ->addHeader('Authorization', "Bearer " . $this->getToken())
    ->attach($files)
    ->body(json_encode($data))
    ->sendsJson()
    ->send();

ファイルを送信したり、本文を送信したりできます。しかし、両方で試してもうまくいきません...

ヒントはありますか?

よろしくn00n

4

2 に答える 2

3

Google経由でこのページにアクセスした方へ。これが私のために働いたアプローチです。

attach() と body() を一緒に使用しないでください。一方が他方をクリアすることがわかりました。代わりに、 body() メソッドを使用してください。file_get_contents() を使用してファイルのバイナリ データを取得し、次にそのデータを base64_encode() して、パラメーターとして $data に配置します。

JSONで動作するはずです。このアプローチは、$req->body(http_build_query($data)); を使用して、application/x-www-form-urlencoded MIME タイプで機能しました。

$this->login();
$filepath = 'aTest1.jpg';
$data =
    array(
        'name'=>'first file',
        'description'=>'first file description',
        'author'=>'test user'
    );
$req = Request::post($this->getRoute('test'))
    ->addHeader('Authorization', "Bearer " . $this->getToken());

if (!empty($filepath) && file_exists($filepath)) {
    $filedata = file_get_contents($filepath);
    $data['file'] = base64_encode($filedata);
}

$response = $req
    ->body(json_encode($data))
    ->sendsJson();
    ->send();
于 2016-08-11T20:19:58.853 に答える