Dingo talk - adampatterson/Dingo-Framework GitHub Wiki

Overview The Dingo talk library allows your Dingo application to communicate with other remote Dingo applications or with other pages inside of your Dingo application in a client/server fasion. To load the talk library manually you may do this:

load::library('talk');

###How it Works Dingo talk requests are regular HTTP requests just like your web browser uses. The only difference is that in addition to sending all the regular HTTP headers talk also sends some POST data used to identify that it is a talk request.

When a page running a Dingo talk server receives a request it checks to see if it contains the POST __talk param. If it does, then instead of sending back any data you have echo'd or loaded from a view, it sends back some other data that you have set.

###Why Use It? Dingo talk allows you to easily create an API for your existing Dingo web application. It only takes one line of code, so why not?

###Setup A Dingo Talk Client Sends a talk request to a talk server.

$url = 'http://www.dingotuts.com/rss/articles/';

echo talk::request($url);

NOTE: talk::request() uses cURL to send a HTTP requests to talk servers, so you must have cURL installed to run a talk client. cURL is not required to run a talk server.

###Setup A Dingo Talk Server A talk server responds only to talk requests. A talk server only takes one line of code to setup:

$response = 'Hello talk client!';

echo 'Hi person!';

talk::respond($response);

If a talk request is sent to the page in your application containing the above code, it will respond with the data Hello talk client!.

If a regular HTTP request (like from your web browser) is sent to the page containing the talk server, then the talk server does nothing and the user will see Hi person!.

As you may have imagined, your talk server can respond in any data format that you wish, such as JSON or XML.

###3rd Party Dingo Talk Clients Chances are that if you are going to have an API for your web app, then you are going to want users that don't have Dingo to be able to use it. Because Dingo talk just uses regular old HTTP requests, it is really easy to make your own talk client.

$url = 'http://www.dingotuts.com/rss/articles/';

$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,'__talk=1');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$response = curl_exec($ch);

###API Keys Many APIs use access keys to identify and authenticate users using the API. You could for example check to see if the user has provided a valid API key like so:

// If is talk request
if(talk::is_request())
{
  // Check to see if key matches
  if(input::post('key') === 'AbC12three')
  {
    // If it matches respond with data
   talk::respond($response);
  }

  // Otherwise respond with an error
  else
  {
    talk::respond('Error: Invalid key!');
  }
}

Then on the client side you would supply the key:

$response = talk::request($url,array('key'=>'AbC12three'));