消费网络服务laravel [英] consuming webservice laravel
问题描述
我在laravel中有两个项目,一个只有我的愿景( AppView ),另一个是与Web服务( AppWs )有关,在我的Web服务中,我有以下项目路线 路由项目AppW 路线:: group(['prefix'=>'api'],function(){
I have two projects in laravel, one only with my vision (AppView) and another with the web service (AppWs), in my web service I have the following route Route project AppWs Route::group(['prefix' => 'api'],function(){
Route::group(['prefix' => 'user'], function(){
Route::group(['prefix' => 'tipoprojeto'], function(){
Route::get('','Painel\TipoProjetoController@All');
});
});
});
当我访问 http://localhost/WebServiceApp/public/api/user/tipoprojeto /
它会向我返回一个包含所有数据的数组,直到此为止.
it returns me an array with all the data, until then all right.
在另一个项目中,我有一个TypeProjectController控制器,并且有我的index()方法( AppView ),那么如何检索要在此处加载的Web服务数据?
in my other project, I have a TypeProjectController controller and I have my index () method (AppView), so how can I retrieve the webservice data to load here?
编辑
AppWs 负责处理数据
public function All(){
return $this->ModelTipoProjeto->paginate(5);
}
AppView 负责显示数据 Route :: resource('/Painel/TipoProjeto','Painel \ TipoProjetoController');
public function index()
{
$getData = `http://localhost/WebServiceApp/public/api/user/tipoprojeto/` // <~~
return view('Painel.TipoProjeto.index');
}
检索AppWebservice链接返回的数据
Retrieve the data that the AppWebservice link returns
推荐答案
首先,要使用外部服务,您必须从http请求向要获取数据表单的端点进行操作.
First of all in order to consume an external service you have to perfrom a http request towards the endpoint where you intend to get the data form.
您的结局:http://localhost/WebServiceApp/public/api/user/tipoprojeto/
安装guzzle,它是用于执行http调用的php curl包装器. 在您的root目录中,打开命令行并通过触发来在项目中插入guzzle:
Install guzzle which is a php curl wrapper to perform http calls. In your root dir open command line and inject guzzle in your project by firing :
composer require guzzlehttp/guzzle
确保通过添加
use GuzzleHttp\Client;
然后转到您的索引方法并执行以下操作:
Then go to your index method and do the following:
public function index(){
// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'http://localhost/WebServiceApp/public/api/user/tipoprojeto/']);
// Send a request to http://localhost/WebServiceApp/public/api/user/tipoprojeto/
$response = $client->request('GET', 'test');
// $response contains the data you are trying to get, you can do whatever u want with that data now. However to get the content add the line
$contents = $response->getBody()->getContents();
dd($contents);
}
$contents
包含数据,您现在可以执行任何操作.
$contents
contains the data now you can do whatever you want.
这篇关于消费网络服务laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!