Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.4-如何从服务器接收JSON数据并保存在数据库中_Php_Json_Database_Web Services_Laravel 5.4 - Fatal编程技术网

Php Laravel 5.4-如何从服务器接收JSON数据并保存在数据库中

Php Laravel 5.4-如何从服务器接收JSON数据并保存在数据库中,php,json,database,web-services,laravel-5.4,Php,Json,Database,Web Services,Laravel 5.4,我正在使用Web服务。我的要求是从服务器接收JSON数据并存储在DB[order table]中。客户使用移动设备下多个订单。同样的订单必须在laravel controller中接收并存储在order table中。我不应该仅通过controller使用表单,我应该接收数据。 我对webservices和JSON这个概念还不熟悉 public function receive(Request $request) { $data = $request->json()-

我正在使用Web服务。我的要求是从服务器接收JSON数据并存储在DB[order table]中。客户使用移动设备下多个订单。同样的订单必须在laravel controller中接收并存储在order table中。我不应该仅通过controller使用表单,我应该接收数据。 我对webservices和JSON这个概念还不熟悉

public function receive(Request $request)
{       
    $data = $request->json()->all();
    $order = new order();
    $order->product_int = $request->input('products');
    $order->quantity_float = $request->input('quantity');
    $order->amount_float = $request->input('amount');       
    $order->save();        
}
在api.php中,我调用
Route::get('/receive','OrderController@receive');

上面的代码不起作用。关于如何接收JSON数据作为函数参数以及如何保存到DB的建议。

您应该使用Laravel中的Input::all方法接收关联数组,如下所示

public function receive(Request $request)
{       
   $data = Input::all(); //$data = $request->json()->all(); should also work
   $order = new order();
   $order->product_int = $data['products'];
   $order->quantity_float = $data['quantity'];
   $order->amount_float = $data['amount'];       
   $order->save();        
}
也许,你会想改变你的路线,用post代替get

Route::post('/receive', 'OrderController@receive'); 
如果您不想提交表单,那么可以使用CURL将数据发布到API

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"YOUR-API-URL-HERE");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('products' => $value, 'quantity' => $quantity, 'amount' => $amount)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);

PHP和/或Laravel错误日志中有什么内容?