I4a API PHP Example: Difference between revisions

From i4a API Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[I4A api guide]]
[[I4A API Guide]]


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




  $username = 'adminuser1';  
  $username = 'adminuser1';  
  $password = 'thepassword';
  $password = 'thepassword';
  $site_token         = 'xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx';
  $site_token = 'xxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx';
  $wsdl_url = 'http://www.mysite.org/i4a/utilities/authenticateAdmin.cfc?wsdl';
  $wsdl_url = 'https://www.mysite.org/i4a/utilities/authenticateAdmin.cfc?wsdl';
  $search_url         = 'http://www.mysite.org/i4a/api/json_noheader/membership.contacts/lastname%20like%20b/';
  $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
  // We need to authenticate, but once we have our authKey we won't need to repeat that
  $client     = new SoapClient($wsdl_url);  
// 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.
  $adminXML   = $client->authenticateAdmin($username,$password,$site_token);
// 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);
  $adminJSON = json_decode(json_encode((array) simplexml_load_string($adminXML)),1);
  dump($adminJSON,"response packet from authentication");
  dump($adminJSON,"response packet from authentication");
Line 50: Line 52:
foreach ($vars as $key => $value) {
foreach ($vars as $key => $value) {
echo "<tr><td bgcolor='#DDDDDD'>$key (".gettype($value).")</td><td>";
echo "<tr><td bgcolor='#DDDDDD'>$key (".gettype($value).")</td><td>";
// if our value is itself an array, let's recursively call dump again so we can
// display the actual values within the arrays...
if (gettype($value) == "array" || gettype($value) == "object") {dump($vars=$value,$label=$key); } else { echo $value; }
if (gettype($value) == "array" || gettype($value) == "object") {dump($vars=$value,$label=$key); } else { echo $value; }
echo "</td></tr>";
echo "</td></tr>";
}
}
}
}
else {
else { echo "<tr><td>".gettype($vars) . "</td><td>"; echo $vars."</td></tr>"; }
echo "<tr><td>".gettype($vars) . "</td><td>";
echo $vars."</td></tr>";
}
echo "</table><br><br>";
echo "</table><br><br>";
}  
}  
Line 66: Line 63:
</source>
</source>


[[category:api]]
[[Category:API]]

Latest revision as of 12:02, 22 September 2022

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>";
} 
  
?>