我有一个驻留在不同服务器上的php应用程序和laravel应用程序。我需要将数据从php应用程序发送到laravel应用程序。可以将这两个应用程序互连并发送数据吗?
我尝试在PHP应用程序中使用curl发送数据:
t
在laravel应用程序中:
在web.php文件中:
$url ='ip/project/public/index.php/admin/update-item-status';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$item);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
在ItemsController.php中:
Route::match(['get','post'], 'admin/update-item-status', 'ItemsController@updateItemStatus');
答案 0 :(得分:2)
是的,您可以使用curl
或GuzzelHttp
发布数据。这些是您可以遵循的步骤。
假设,您的应用托管如下:
Laravel: http://xyz.domain
现在在laravel应用中:
post
中创建一条routes/api.php
路线:Route::post('receive_data', 'CommunicationController@receive');
receive
中定义动作CommunicationController
,如下所示:public function receive(Request $request)
{
dd($request); // just to debug data.
// do whatever you want.
}
在PHP应用程序中:
curl
请求以发布数据。$url ='ip/project/api/admin/update-item-status';
$payload = json_encode( array( "item"=> $data ) );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);