1. Home
  2. Docs
  3. Simple API
  4. Example: Building a PHP P...
  5. Sample PHP Code

Sample PHP Code

The following code provides basic cover functions that call the REST API for lists and fields, to enable configuration, and then a simple function call to add the email to the list. Copy, paste, add the API key, and you’re good to go.

IMPORTANT: Use the X-Forwarded-For Header

The X-Forwarded-For header in particular helps FeedBlitz determine the IP and other characteristics of the client. In a solution where the request chain is something like this:

Browser -> Your End User's Site -> Your Servers -> FeedBlitz

Then we ideally want to know the IP address of the host at the very far end, the browser. This is what x-forwarded-for delivers, and it is very good practice when interacting with APIs to provide / extend it as early as possible. The sample code below is generic and doesn’t set this header; you need to add that based on what you know about the remote visitor. See https://stackoverflow.com/questions/12404497/how-to-set-x-forwarded-for-in-php-to-sent-client-ip-with-simplexml-load-file/12404729 for the right CURLOPT syntax.

<?php 
// helper function to access the FeedBlitz API via GET
function fbz_get_web_page( $url )
{
  $options = array(
    CURLOPT_RETURNTRANSFER => true, // return web page
    CURLOPT_HEADER => false,        // don't return headers
    CURLOPT_FOLLOWLOCATION => true, // follow redirects
    CURLOPT_ENCODING => "",         // handle all encodings
    CURLOPT_USERAGENT => "PHP FeedBlitz Web Form Handler", // a UA is required
    CURLOPT_AUTOREFERER => true,    // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect
    CURLOPT_TIMEOUT => 120,         // timeout on response
    CURLOPT_MAXREDIRS => 10,        // stop after 10 redirects 
    CURLOPT_SSL_VERIFYPEER => false,
  );
   
  $ch = curl_init( $url ); 
  curl_setopt_array( $ch, $options ); 
  $content = curl_exec( $ch ); 
  $err = curl_errno( $ch ); 
  $errmsg = curl_error( $ch ); 
  curl_close( $ch ); 
  return $content;
}

// turns the returned XML into a PHP array
function fbz_prepare_results($page)
{
  $xml = simplexml_load_string($page, "SimpleXMLElement", LIBXML_NOCDATA);
  $json = json_encode($xml);
  $array = json_decode($json,TRUE);
  return $array;
}

// returns an array of list IDs, names and current subscriber counts
function fbz_get_lists($api_key, $lists)
{
  $url = "https://app.feedblitz.com/f.api/syndications?key=" . $api_key ."&summary=1&status=ok"; 
  $page = fbz_get_web_page($url);
  $array = fbz_prepare_results($page);
  if($array['rsp']['@attributes']['stat']=="ok")
  {
    $lists_array = $array['syndications']['syndication'];
    if ( ! empty( $lists_array ) ) {
      foreach ( $lists_array as $list_data ) {
        $lists[ $list_data['id'] ]['id']        = $list_data['id'];
        $lists[ $list_data['id'] ]['name']      = $list_data['name'];
        $lists[ $list_data['id'] ]['subscribers_count'] = $list_data['subscribersummary']['subscribers'];
      }
    }
  }
  
  return $lists;
}

// get basic custom fields info for the account
function fbz_get_fields($api_key, $fields)
{
  $url = "https://app.feedblitz.com/f.api/fields?key=" . $api_key ."&status=ok"; 
  $page = fbz_get_web_page($url);
  $array = fbz_prepare_results($page);
  if($array['rsp']['@attributes']['stat']=="ok")
  {
    $fields_array = $array['fields']['field'];
    if ( ! empty( $fields_array ) ) {
      foreach ( $fields_array as $field_data ) {
        $fields[ $field_data['id'] ]['id']          = $field_data['id'];
        $fields[ $field_data['id'] ]['name']        = $field_data['name'];
        $fields[ $field_data['id'] ]['description'] = $field_data['description'];
        $fields[ $field_data['id'] ]['hidden']      = $field_data['hidden'];
      }
    }
  }
  
  return $fields;
}
// starts the subscription process for the specified email address
function fbz_subscribe($api_key, $email, $listid, $tags)
{
  $url="https://app.feedblitz.com/f/?SimpleApiSubscribe&key=" . $api_key . "&email=" . $email . "&listid=" . $listid;
  if($tags!="") {$url=$url . "&tags=" . $tags;}
  $page = fbz_get_web_page($url);
  $xml = simplexml_load_string($page, "SimpleXMLElement", LIBXML_NOCDATA);
  $json = json_encode($xml);
  $array = json_decode($json,TRUE);
  return $array['rsp']['@attributes']['stat']=="ok";  // true if all is well, false otherwise
}

// example: Get a publisher's lists
$apikey = "{{apikey}}";                  // Replace {{apikey}} with the FeedBlitz user's API key.
$lists = fbz_get_lists($apikey,$lists);  // gets all the active lists
var_dump($lists);                        // for debugging
?>