I4a API PHP Example

From i4a API Wiki
(Redirected from I4a api PHP Example)
Jump to navigation Jump to search

I4A API Guide

The following code will connect and retrieve all the contacts who have a lastname that starts with a b.

<?php 

/*
 * For the return format section of the URL, the following options are available:
 * 		json 			- returns a json packet WITH header.  The header includes the content type set to application/json
 * 		json_noheader 	- returns a json packet with NO header.  PHP seems to cannot decode a json packet if it includes 
 * 							a header, the header may be needed depending on how this API is used.
 * 		xml 			- returns an XML packet WITH header.  This will return the content type set to text/xml
 * 		xml_noheader 	- returns an xml packet with NO header.  This may be needed depending on your usage.
 * 		wddx			- an XML packet formatted in the WDDX specification. It includes a header set to text/xml.
 * 		wddx_noheader	- xml packet formatted to WDDX spec, does not include header.
 * 
 */


 $username = 'adminuser1'; 
 $password = 'thepassword';
 $site_token = 'xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx';
 $wsdl_url = 'https://www.mysite.org/i4a/utilities/authenticateAdmin.cfc?wsdl';
 $search_url = 'https://www.mysite.org/i4a/api/json_noheader/membership.contacts/lastname%20like%20b/';
 
  
 // We need to authenticate, but once we have our authKey we won't need to repeat that
 // The cache_wsdl option may not be needed, but it was in testing for us and possibly other options may be required for this to work.
 // For more information see the documentation at http://www.php.net
 $client = new SoapClient($wsdl_url,array('cache_wsdl'=>WSDL_CACHE_NONE) ); 
 $adminXML = $client->authenticateAdmin($username,$password,$site_token);
 $adminJSON = json_decode(json_encode((array) simplexml_load_string($adminXML)),1);
 dump($adminJSON,"response packet from authentication");
 $authKey = ($adminJSON['@attributes']['authKey']);
 echo '<hr>authKey='.$authKey.'/<br/>';

 // now we've authenticated and have our key, let's access the API...
 echo 'API URL='.$search_url . "<br>";
 echo "url string=".$search_url.$authKey . "<br>";
 // retrieve the http output into a variable.  Could use cURL functions as well most likely.
 $contactJSON = file_get_contents($search_url.$authKey);
 echo "<hr>Json Packet:<br>";
 echo $contactJSON;


function dump($vars,$label='') {
	echo "<table border=1>";
	echo "<tr><td colspan=2><b>$label</b></td></tr>";
	echo "<tr><td bgcolor='#088A85'>Variable</td><td bgcolor='#088A85'>Value</td></tr>";
	if (gettype($vars) == "array" || gettype($vars) == "object") {
		foreach ($vars as $key => $value) {
			echo "<tr><td bgcolor='#DDDDDD'>$key (".gettype($value).")</td><td>";
			if (gettype($value) == "array" || gettype($value) == "object") {dump($vars=$value,$label=$key); } else { echo $value; }
			echo "</td></tr>";
			}
		}
	else { echo "<tr><td>".gettype($vars) . "</td><td>"; echo $vars."</td></tr>"; }
	echo "</table><br><br>";
} 
  
?>