Talk to us
Status:

Support Center

Creating a deal using the REST API and PHP (full working example code)

Pipedrive REST API Full Documentation - https://developers.pipedrive.com/docs/api/v1/

Below, you will find a full working example code using our REST API used to create a new deal and its contacts.
 

// your API token goes here
$api_token = "x";
 
 
// main data about the organization
$organization = array(
 'name' => 'Los Pollos Hermanos',
 'address' => '4257 Isleta Blvd SW, Albuquerque, NM 87105, United States'
);
 
// main data about the person. org_id is added later dynamically
$person = array(
 'name' => 'Gus Fring',
 'email' => 'gus@lospolloshermanos.com',
 'phone' => '555-818-1234'
);
 
// main data about the deal. person_id and org_id is added later dynamically
$deal = array(
 'title' => 'Los Pollos Hermanos deal',
 'value' => '500'
);
 
// try adding an organization and get back the ID
$org_id = create_organization($api_token, $organization);
 
// if the organization was added successfully add the person and link it to the organization
if ($org_id) {
 $person['org_id'] = $org_id;
 // try adding a person and get back the ID
 $person_id = create_person($api_token, $person);
 
 // if the person was added successfully add the deal and link it to the organization and the person
 if ($person_id) {
 
  $deal['org_id'] = $org_id;
  $deal['person_id'] = $person_id;
  // try adding a person and get back the ID
  $deal_id = create_deal($api_token, $deal);
 
  if ($deal_id) {
   echo "Deal was added successfully!";
  }
 } else {
  echo "There was a problem with adding the person!";
 }
 
} else {
 echo "There was a problem with adding the organization!";
}
 
//create_person($api_token, $person);
//create_deal($api_token, $deal);
 
function create_organization($api_token, $organization)
{
 $url = "https://api.pipedrive.com/v1/organizations?api_token=" . $api_token;
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, true);
 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $organization);
 $output = curl_exec($ch);
 $info = curl_getinfo($ch);
 curl_close($ch);
 // create an array from the data that is sent back from the API
 $result = json_decode($output, 1);
 // check if an id came back
 if (!empty($result['data']['id'])) {
  $org_id = $result['data']['id'];
  return $org_id;
 } else {
  return false;
 }
}
 
function create_person($api_token, $person)
{
 $url = "https://api.pipedrive.com/v1/persons?api_token=" . $api_token;
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, true);
 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $person);
 $output = curl_exec($ch);
 $info = curl_getinfo($ch);
 curl_close($ch);
 
 // create an array from the data that is sent back from the API
 $result = json_decode($output, 1);
 // check if an id came back
 if (!empty($result['data']['id'])) {
  $person_id = $result['data']['id'];
  return $person_id;
 } else {
  return false;
 }
}
 
function create_deal($api_token, $deal)
{
 $url = "https://api.pipedrive.com/v1/deals?api_token=" . $api_token;
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, true);
 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);
 $output = curl_exec($ch);
 $info = curl_getinfo($ch);
 curl_close($ch);
 
 // create an array from the data that is sent back from the API
 $result = json_decode($output, 1);
 // check if an id came back
 if (!empty($result['data']['id'])) {
  $deal_id = $result['data']['id'];
  return $deal_id;
 } else {
  return false;
 }
}