O'Reilly logo

CakePHP 2 Application Cookbook by Jorge González, James Watts

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required