Consuming a service
While it's great that you can expose certain functionality of your application via web services, you'll also want to consume services of your own or by other third parties. CakePHP comes equipped with a very useful HTTP socket class, which allows you to easily interact with other resources.
In this recipe, we'll consume the service from our previous example, which exposes its interface as JSON, and see how easy it is to quickly get the results you need.
Getting ready
For this recipe, we'll use a simple test controller to interact with our API. Create a file named ServiceController.php
in app/Controller/
with the following content:
<?php App::uses('AppController', 'Controller'); App::uses('HttpSocket', 'Network/Http'); class ServiceController extends AppController { }
We'll also need a view file for the action we'll define, so create a file named call.ctp
in app/View/Service
.
How to do it...
Perform the following steps:
Create a file named
JsonResponse.php
inapp/Lib/Network...