duancaiyi7567 2017-01-02 16:35
浏览 95
已采纳

Laravel XML下载不正确导出

I'm still learning Laravel 5.3 and am using Guzzle to connect to an API to download an already formatted xml file that I need to save to local user's pc for further usage.
I have created a download button:

    <a href="{{ '/vendorOrder' }}" class="btn btn-large pull-right> Download Order </a>

I created a controller called OrderController.php:

    <?php
     namespace App\Http\Controllers\edi;

     use Illuminate\Http\Request;
     use App\Http\Controllers\Controller;
     use GuzzleHttp\Exception\GuzzleException;
     use GuzzleHttp\Client as GuzzleHttpClient;

     class OrderController extends Controller
     {
          public function vendorOrder()
          {
              try {
                   $filename = 'order.xml';
                   $path = storage_path($filename)';

                   $client = new GuzzleHttpClient();
                   $apiRequest = $client->request('GET', 'Https://urlapi') [
                   'headers' => [
                       'Authorization' => 'Basic QVBJVGVzdFVzZXIsIFdlbGNvbWVAMTIz',
                       'ContractID' => 'aa659aa2-4175-471f',
                       'Accept' => 'text/xml'
                   ],
                   ]);

               $content = ($apiRequest->getBody()->getContents());

               return response::download(file_put_contents($path, $content), '200', [
               'Content-Type' => 'text/xml',
               'Content-Disposition' => 'attachment; filename="'.$filename.'"'
             ]);

              } catch (RequestException $re) {
                 echo $re;
              }
           }
       }

and the route:

    Route::get('/vendorOrder', 'edi\OrderController@vendorOrder');        

I'm able to connect just fine, and the contents of the xml display just fine when I use:

    return response($content, '200')->header('content-type', 'text/xml');

however when I use:

    return response::make(file_put_contents($path, $content), '200', [
            'Content-Type' =>  'text/xml',
            'Content-Disposition' => 'attachment; filename="'.$filename.'"'
        ]);

as in controller above, I'm able to download a file called order.xml (as expected) but the contents is just a single number ie "1179866". No xml tags or the xml content or anything else - just the number.

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • duanlieshuang5330 2017-01-02 18:39
    关注

    In both cases you are wrong.

    1. response::download What are the arguments to response::download? See a manual:

    Response::download($pathToFile);

    See - path to file. In your code:

    return response::download(file_put_contents($path, $content), // other arguments
    

    result of file_put_contents is not a path to file.

    Read a manual and see that file_put_contents

    This function returns the number of bytes that were written to the file, or FALSE on failure.

    Solution:

    $path = 'my/path/here';
    file_put_contents($path, $content);
    return response::download($path, // other arguments
    
    1. response::make. Same error.

    See a manual:

    response::make($contents, $statusCode);

    Again - first argument is $contents.

    In your code

    response::make(file_put_contents($path, $content),
    

    contents if the result of file_put_contents execution, which is see above.

    Solution:

    response::make($content, 200, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度