Clients are always looking for more leads. This example demonstrates how to create a simple “Generate Leads” button that adds fake leads into an IDX Broker account using the API.
Note: These are not real leads. This project is intended as a development example for working with the IDX Broker API.
This lesson uses:
- PHP
- cURL
- The IDX Broker API
- Faker-generated test data
You should already have a basic understanding of PHP, cURL, and making API calls before beginning.
Step 1: Create the Form
Start by creating a file called:
index.php
This form accepts an IDX Broker API key and submits it back to itself.
<html>
<head>
</head>
<body>
<?php
echo '<form action="">
<h1>Generate Lead</h1>
<input type="text" name="apikey" placeholder="API Key"><br/><br/>
<button type="submit">
<img src="assets/push.jpg" />
</button>
</form>';
?>
</body>
</html>
This gives users a simple button interface for triggering the lead-generation process.
Step 2: Validate the API Key
API calls made with invalid keys still count against API rate limits, so it is important to validate the key before making requests.
IDX Broker API keys are always 22 characters long.
Update your script to check the key length before proceeding:
<html>
<head>
</head>
<body>
<?php
// Get API key from URL
$APIKey = $_GET["apikey"];
// Check that it is 22 characters
$check = strlen($APIKey);
if ($check == 22){
// API calls will go here.
}
else{
echo '<form action="">
<h1>Generate Lead</h1>
<input type="text" name="apikey" placeholder="API Key"><br/><br/>
<button type="submit">
<img src="assets/push.jpg" />
</button>
</form>';
}
?>
</body>
</html>
Step 3: Generate Fake Lead Data
Next, use Faker endpoints to generate random names and email addresses.
The script:
- Retrieves a fake full name
- Splits it into first and last name values
- Retrieves a fake email address
- Sends the data into IDX Broker using the Leads API
Complete Example Script
<html>
<head>
</head>
<body>
<?php
// Get API key from URL
$APIKey = $_GET["apikey"];
// Check that it is 22 characters
$check = strlen($APIKey);
if ($check == 22){
// Get random fake name
$ch = curl_init('http://faker.hook.io?property=name.findName');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$name = curl_exec($ch);
$names = explode(" ", $name);
$firstname = $names[0];
$lastname = $names[1];
// Get random fake email
$ch = curl_init('http://faker.hook.io?property=internet.email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$email = curl_exec($ch);
// PUT lead into IDX Broker
$url = 'https://api.idxbroker.com/leads/lead';
$data = array(
'firstName' => $firstname,
'lastName' => $lastname,
'email' => $email
);
$data = http_build_query($data);
$method = 'PUT';
// Headers
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
'accesskey: '.$APIKey,
'outputtype: json'
);
// Set up cURL
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
if ($method != 'GET')
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method);
// Send data
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
// Execute request
$response = curl_exec($handle);
// Get HTTP response code
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($code >= 200 || $code < 300){
$response = json_decode($response, true);
// Success message
echo 'Lead '.$email.' was added to your IDX Broker account.<br><br>
<img src="assets/leads.gif">';
}
else{
echo $code.' Lead Not Added';
}
}
else{
echo '<form action="">
<h1>Generate Lead</h1>
<input type="text" name="apikey" placeholder="API Key"><br/><br/>
<button type="submit">
<img src="assets/push.jpg" />
</button>
</form>';
}
?>
</body>
</html>
Notes
- A successful API response will typically return an HTTP status code in the
200range. - Errors will generally return
400level or higher status codes. - This example intentionally writes out each cURL request individually for learning purposes.
- In production code, you would typically create reusable cURL helper functions instead.
Final Thoughts
While generating fake leads is not a practical real-world workflow, this lesson demonstrates an important development concept:
Retrieving data from one source and pushing it into IDX Broker through the API.
This same process can be adapted for integrations with:
- CRMs
- Social media platforms
- Lead generation services
- Import lists
- Third-party applications
How you retrieve the external data may vary, but the overall integration concept remains the same.