<?php
/*
* Uses PHP 5 but not JSON
*/
$GLOBALS['api_url'] = "[URL unfurl="true"]https://demo.rpmsoftware.com/rpm/Api.svc/";[/URL]
$GLOBALS['api_key'] = "7403bd28-f709-424f-b14d-0a62f82622fc";
/*
* An array with the customer data to add
*
*/
$customer = array(
'name' => $_GET['name'],
'website' => $_GET['website'],
'address' => $_GET['address'],
'city' => $_GET['city'],
'stateprovince' => $_GET['state'],
'zippostalcode' => $_GET['zip'],
'salutation' => $_GET['salutation'],
'first name' => $_GET['firstname'],
'last name' => $_GET['lastname'],
'title' => $_GET['title'],
'email' => $_GET['email'],
'phone' => $_GET['phone'],
// To use custom fields make 'fields' an array like this:
'fields' => array(
'comments' => $_GET['comments']
)
);
/*
* A generic POST function
*
* This isn't specific to RPM, it's just a simple POST wrapper to
* fopen. Any post function will do and something using CURL is
* probably better: [URL unfurl="true"]http://curl.haxx.se/libcurl/php/[/URL]
*
*/
/*
function req_post($url, $data, $optional_headers = null) {
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
return $response;
}
*/
function req_post($url, $data, $optional_headers = null) {
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
/* establish CURL options */
$options = array(
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_MAXREDIRS=>5,
CURLOPT_FRESH_CONNECT=>true,
CURLOPT_FORBID_REUSE=>true,
CURLOPT_POST=>$params['method'],
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_SSL_VERIFYHOST=>false,
CURLOPT_TIMEOUT=>10);
/* in case GET method is used, transform data array into query string */
switch (strtolower($params['method'])):
case 'get':
if(is_array($data)):
$string = array();
foreach ($params['content'] as $key=>$value):
$string[] = urlencode($key) .'='.urlencode($value);
endforeach;
$params['content'] = implode ('&', $string);
endif;
$url .= '?'.$params['content'];
break;
case 'post':
$options[CURLOPT_POSTFIELDS]= $params['content'];
break;
endswitch;
/* initiate curl handle */
$cH = curl_init($url);
/* set up curl options */
curl_setopt_array($cH, $options);
/* test for additional option */
/* note these must now be in the form of CURL options */
if ($optional_headers !== null && is_array($optional_headers)):
foreach ($optional_headers as $key=>$value):
$r = @curl_setopt($cH, $key, $value);
if($r === false):
throw new Exception ('Error setting option '.$key .' to value ' . $value .'. Error reported: ' . curl_error());
endif;
endforeach;
endif;
/* execute request */
$response = curl_exec($cH);
echo "FOO: $response ";
if ($response === false){
throw new Exception("Problem reading data from $url. \nError was " . curl_error($cH));
} else {
return $response;
}
}
/*
* API call without JSON functions
*
*/
function rpm_req($call, $data_string = "") {
if ("" != $data_string) $data_string = ", ".$data_string;
// Build the request
$data = '{"Key":"'.$GLOBALS['api_key'].'" '.$data_string.' }';
// Make the request
$result = req_post($GLOBALS['api_url'].$call, $data, 'Content-Type: application/json; charset=utf-8');
// Analyse the results
$response_pattern = '/\{\"[A-Za-z]+\":\"(.+)\"\}/';
$error_pattern = '/\{\\\"Error\\\":\\\"([A-za-z0-9 ]+)\\\"\}/';
if (preg_match($response_pattern, $result, $matches)) {
$return = $matches[1];
if (preg_match($error_pattern, $return, $matches)) {
throw new Exception("API returned error: $matches[1]");
}
return $return;
} else {
throw new Exception("Problem with return from $url");
}
}
?>
[CODE]<html>
<head>
<title>Add customer API example</title>
<style>
body { background: #eee; font-family: Arial, sans-serif; font-size: small; }
</style>
</head>
<body>
<?php
if (is_array($customer['fields'])) {
$customer_fields = ', "Fields": [';
foreach ($customer['fields'] as $field_name => $field_value) {
$customer_fields .= '{"Field":"'.$field_name.'","Value":"'.$field_value.'"},';
}
$customer_fields = substr($customer_fields, 0, strlen($customer_fields) - 1); // chop off the last ,
$customer_fields .= ']';
} else $customer_fields = "";
try {
$result = rpm_req('CustomerAdd','
"Customer": {
"Name": "'.$customer['name'].'",
"Website": "'.$customer['website'].'",
"Address": "'.$customer['address'].'",
"City": "'.$customer['city'].'",
"StateProvince": "'.$customer['stateprovince'].'",
"Country": "United States",
"ZipPostalCode": "'.$customer['zippostalcode'].'",
"PrimaryContact": {
"Salutation": "'.$customer['salutation'].'",
"FirstName": "'.$customer['first name'].'",
"LastName": "'.$customer['last name'].'",
"Title": "'.$customer['title'].'",
"Email": "'.$customer['email'].'",
"PhoneBusiness": { "Number":"'.$customer['phone'].'"}
}
'.$customer_fields.'
}
');
echo 'Customer added';
} catch (Exception $e) {
echo $e->getMessage();
}
?>
</body>
</html>