#file.php '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"); } } ?>