#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use PHP::Include;
include_php_vars('file.php');
PHP::Include
makes it really easy. You just need a little file.php
#file.php
<?php
/*
* Add customer example code
* Uses PHP 5 but not JSON
*
*/
$GLOBALS['api_url'] = "https://demo.rpmsoftware.com/rpm/Api.svc/";
$GLOBALS['api_key'] = "xxxxx";
/*
* An array with the customer data to add
*
*/
$customer = array(
'name' => '111A Test Customer1',
'website' => '',
'address' => '',
'city' => '',
'stateprovince' => '',
'zippostalcode' => '',
'salutation' => '',
'first name' => '',
'last name' => '',
'title' => '',
'email' => '',
'phone' => '',
// To use custom fields make 'fields' an array like this:
// 'fields' => array(
// 'fieldname' => 'fieldvalue'
// );
//
// Example:
// 'fields' => array(
// 'Custom text' => 'a demo value',
// 'Some date' => '12/20/2009',
// );
'fields' => ''
);
/*
* 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: http://curl.haxx.se/libcurl/php/
*
*/
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;
}
/*
* 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");
}
}
?>
<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":"'.$fi
+eld_value.'"},';
}
$customer_fields = substr($customer_fields, 0, strlen($customer_fi
+elds) - 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>
|