All Collections
Automation
Creating records using the Sheep API
Creating records using the Sheep API

Write your own code to link into Sheep's API

James Scholes avatar
Written by James Scholes
Updated over a week ago

As well as supporting direct integration with many other web systems, Sheep has a rich API protocol which can be used to manage your flock. This article demonstrates a potential method of linking a webform to Sheep to automatically create records.

In PHP:

<?php
//take a form with a handful of questions, some of which are relevant to Sheep

//set up some values
$flockname = 'your-flock-name'; //set this to the name of your flock
$bearer = 'mYt0pS3cR3tAP1k3yZZ11!1'; //set this to your Sheep API key


//capture form data and convert it to php variable
$obj = json_decode($_REQUEST['rawRequest'], true);

// for debugging to a file, uncomment this.
//file_put_contents("/tmp/newbie.txt", $_REQUEST['rawRequest'] );

$name = $obj['q1_name']; // name field is a compound field in the form
$first = $name['first']; // with two parts "first" and "last"
$last = $name['last'];
$handle = $obj['q2_nickname']; // get nickname
$email = $obj['q3_emailAddress']; //get email
$mobile = $obj['q5_phoneNumber']; //get phone number
$status = $obj['q8_signupType']; // get status
$date = date("F jS, Y"); // get date

updateSheep($flockname,
            $bearer,
            $first,
            $last,
            $handle,
            $email,
            $mobile,
            $status,
            $date);

function updateSheep($flockname,
                     $bearer,
                     $first,
                     $last,
                     $handle,
                     $email,
                     $mobile,
                     $status,
                     $date){
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sheepcrm.com/api/v1/".$flockname."/person/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => '{"first_name":"'.$first.'", '.
                           '"last_name":"'.$last.'", '.
                           '"email": "'.$email.'", '.
                           '"known_as":"'.$handle.'", '.
                           '"telephone": "'.$mobile.'", '.
                           '"tags" : "newbie, '.$status.'", '.
                           '"source" : "Signed up as '.$status.' on '.$date.'."}',
    CURLOPT_HTTPHEADER => array(
      "authorization: Bearer ".$bearer,
      "cache-control: no-cache"
      ),
  ));

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
    error_log("cURL Error #:" . $err, 0);
  } else {
    echo $response;
  }
}
?>

More examples in other languages will be added to this article. Please let us know if you have specific requirements.

Did this answer your question?