API - PHP Example

This example shows a complete API call in PHP using cURL. It creates a customer from a submitted web form and reads back the new Customer ID. You can use it as a starting point for any other API call.


Before You Start

  • You need an API key with Customers set to Full Access. See API - Setup Access.
  • Keep your API key on your server. Never put it in JavaScript or anywhere a website visitor could see it.

Create a Customer

<?php
$curl_handle = curl_init();

// The data to send. The API key goes in the POST body with the other fields.
$post_data  = 'apiKey=YOUR_API_KEY';
$post_data .= '&name=' . urlencode($_POST['name']);
$post_data .= '&email=' . urlencode($_POST['email']);
$post_data .= '&cellphone=' . urlencode($_POST['phone']);
$post_data .= '&customertype=' . urlencode($_POST['type']);

curl_setopt($curl_handle, CURLOPT_URL, 'https://rental.software/api6/customers/');
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 15);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

$output_json = curl_exec($curl_handle);
$httpCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);

// 201 = customer created, 409 = customer already exists.
// Both give you a customer ID in recordid.
if ($httpCode != 201 && $httpCode != 409) {
    // Something went wrong. $output_json holds the error message.
    error_log('API error ' . $httpCode . ': ' . $output_json);
    exit;
}

$output = json_decode($output_json);
$customer_id = $output->recordid;

How It Works

  1. Build the data. Each field is added to $post_data as name=value. urlencode() makes sure spaces and special characters are sent safely.
  2. Send the request. CURLOPT_POST makes it a POST request and CURLOPT_RETURNTRANSFER returns the answer as a string instead of printing it.
  3. Check the HTTP status code. Always check the code before you use the answer. Here 201 and 409 both mean you have a customer to work with.
  4. Read the answer. The answer is JSON. json_decode() turns it into a PHP object, and recordid holds the Customer ID.

A successful answer looks like this:

{
  "status": 201,
  "message": "Created",
  "recordid": 533297,
  "request_time": 1760000000
}

Next Step: Create a Lead for That Customer

Use the Customer ID from above as customerid when you create a lead:

<?php
$post_data = http_build_query(array(
    'apiKey'       => 'YOUR_API_KEY',
    'customerid'   => $customer_id,
    'eventname'    => 'Birthday Party',
    'eventstartdate_text' => '7/25/2026',
    'eventstarttime_text' => '2:00 pm',
    'eventduration' => 2,
    'eventzip'     => '44444',
    'rental_names' => 'Bounce House-1-125',
));

$curl_handle = curl_init('https://rental.software/api6/leads/');
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$output = json_decode(curl_exec($curl_handle));
$httpCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);

if ($httpCode == 201) {
    $lead_id = $output->recordid;
}

http_build_query() builds the same kind of string as the first example and does the URL encoding for you.


Related Links

Is this article helpful?
0 0 0