将文件上传的POST请求从一个Laravel应用程序重新发送到第二个Laravel应用程序

时间:2014-06-29 17:23:35

标签: php laravel-4

我正在尝试将信息从一个laravel应用程序中的表单发布到第二个laravel应用程序。文字很好,问题是重新发送照片。

我现在正在routes.php文件中对此进行测试,以便不会将请求发送到单独的应用程序,而是将请求发送给自己。这是我的代码:

Route::get('testpost', function() {
    $html = '<form action="testpost" enctype="multipart/form-data" method="post"><input type="file" accept="image/*" value="Upload Photo" name="photo" id="photo"><input type="submit" value="Submit" name="submit" id="submit"></form>';
    return $html;
});
Route::post('testpost', function() {
    $guzzle = new \GuzzleHttp\Client();
    $request = $guzzle->createRequest('POST', 'https://test1.dev/secondpost', [
        'config' => [
            'curl' => [
                CURLOPT_SSL_VERIFYPEER => false,
            ]
        ]
    ]);
    $postBody = $request->getBody();
    $photo = Input::file('photo');
    $postBody->addFile(new \GuzzleHttp\Post\PostFile('photo', $photo));
    try {
        $response = $guzzle->send($request);
        echo $response;
    } catch(Exception $e) {
        echo $e->getMessage();
    }
});
Route::post('secondpost', function() {
    if(Input::hasFile('photo')) {
        var_dump(Input::file('photo'));
    } else {
        echo 'No photo';
    }
});

我得到的输出是:

object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]
  private 'test' => boolean false
  private 'originalName' => string 'photo' (length=5)
  private 'mimeType' => string 'text/plain' (length=10)
  private 'size' => int 26
  private 'error' => int 0

即。图像实际上没有重新上传。任何人都知道如何使这个工作?

1 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

$photo = Input::file('photo');
$filename = $photo->getClientOriginalName();
$photo->move(storage_path(), $filename);

然后

$request = $guzzle->createRequest();
$postBody = $request->getBody();
$postBody->addFile(new \GuzzleHttp\Post\PostFile('photo', fopen(storage_path() . '/' . $filename, 'r')));