<?php
/*

PHP Pownce Basic Authentication



There are a few steps in getting Pownce to communicate with your PHP application. I've had some time to work with the new API 
release and am presenting the information here so that others may get their applications up in a quicker manner. An oAuth 
document for use with Pownce and PHP will be released in the future as well.

Without further ado...

Pownce offers two forms of Authentication for use with its API, Basic HTTP and oAuth. 

In order to successfully spawn connections and data transfer from Pownce the easiest method is Basic HTTP. With a fun little module 
called libcurl we can get this up and running in no time.

Pownce  says it would like to see an app_key and a basic authentication header per get/post. This is where curl comes into play.

Curl allows us to connect and communicate with many different types of servers in many different types of protocols (php.net/curl). In 
order to get this working with Pownce we can follow a little guidelines from the API.

"With each authenticated request, the parameter app_key must also be included. The app_key is the application key assigned when the 
application was registered.
Developers will need to construct and send the appropriate authorization header. To construct the header:
append the username and password like username:password
base-64 encode the username:password
send the "Authorization" header with the value "Basic x" where x is the base-64 encoded username:password"
(api.pownce.com)


So lets make a connection..
*/



 
function pownce_data($config_datasource$type 'post'$post_data NULL) {
  
  
$config_address   'http://api.pownce.com/2.0/'// Api link
  
$username         'currentusername';
  
$password         'currentpassword';
  
$config_userpass  base64_encode($username.':'.$password); //Encode userinfo
  
$config_address   .= $config_datasource// Appened our datasource to url
  
$headers[]        = "Authorization: Basic ".$config_userpass//set our header
  
$chandle          curl_init(); //Initialize curl resource
  
  
curl_setopt($chandleCURLOPT_RETURNTRANSFERtrue); 
  
curl_setopt($chandleCURLOPT_HTTPHEADER$headers); //Append our headers to curl resource
  
curl_setopt($chandleCURLOPT_URL$config_address); //Give curl resource our url
  
if(!empty($type) && strtolower($type) == 'post') {
    
curl_setopt($chandleCURLOPT_POST1); //Set curl to POST method.
  
}
  if(!empty(
$post_data) ){
    
curl_setopt($chandleCURLOPT_POSTFIELDS$post_data);
  }
  
  return 
$chandle;
}


//Usage scenario would be :
$post_data 'app_key=xdkjhi3787asd98asd839hs9das8ha98&note_to=public&note_body=testingdocumentation';
$pownce_curl pownce_data('send/message.xml','post',$post_data);

//This will give us our curl resource back with all the connection information, now to make the connection and get the data.

$pownce_xml curl_exec($pownce_curl);
curl_close($pownce_curl);

/*
From then on you can parse the xml using either Dom or SimpleXML
This is the easiest method for which you can connect to Pownce with PHP.
If you have any questions you can email me at nomspes@gmail.com

Happy Powncing!
*/

?>