通过PHP URL传递数据收集

时间:2017-01-04 05:40:28

标签: php laravel url collections

假设我有$data,这是一个表的整个数据的集合,是否可以通过URL从视图到控制器将这个集合类型数据传递给参数?

我正在使用Laravel。我一直只搜索阵列类型数据的所有解决方案。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

你可以。首先安装包guzzlehttp/guzzle。然后尝试这种方式:

use GuzzleHttp\Client;

$client = new Client();
$sampleData = ['name' => 'billy', 'email' => 'billy@example.com']; // your collection
$url = 'http://api.example.com/bla-bla'; // your url

$res = $client->request('POST', "{$url}",['form_params' => $sampleData]);
$data = json_decode(json_encode($res->getBody()->getContents()),true);
return $data;

答案 1 :(得分:0)

从您的视图发送请求到所需的网址。

实施例: 改编自here

<table id="tData">
     <tbody>
         <tr>
             <td class='dataVal1'>100</td>
      ...

$(document).ready(function() {
    var toServer = {};
    var data = $('#tData tbody tr td').each(function(key, value) {
        toServer[$(this).attr('id')] = $(this).text();
    });
    $.ajax({
        url: '/test/',
        data: {
                "_token": "{{ csrf_token() }}",
                "table_data": toServer,
              }
        type: 'POST'
    })
});

现在在处理页面的控制器中,使用以下

public function test(Request $request)
{
  dd($request);
}

注意:确保您在ajax请求中提到的URL可以接受发布请求。