API Example Code

This is simple PHP example code to interact with the Crowdsignal API. A Crowdsignal account is required, as well as an API key. More information on the API can be found in the documentation.

<?php

 /*
  * Example PHP code that uses the Crowdsignal API to:
  * 1. Enter a username, password and API key to get a userCode.
  * 2. List polls
  * 3. View information about a single poll.
  * 4. Create a test poll in your account.
  *
  * Requirements: PHP 4+, libcurl.
  * Copyright: Automattic Inc.
  */ 

$action = isset( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : '';
  switch( $action ) {
  case "getpoll":
    /*
     * Get information about a poll.
     */
     pd_api_menu( $_POST[ 'partnerGUID' ], $_POST[ 'usercode' ] );
     $curl_data = json_encode(
     array(
       "pdRequest" => array(
         "partnerGUID" => $_POST[ 'partnerGUID' ],
         "userCode" => $_POST[ 'usercode' ],
         "demands" => array(
           "demand" => array(
             "poll" => array(
               "id" => (int)$_POST[ 'id' ]
             ),
             "id" => "GetPoll"
           )
         )
       )
     )
   );
   echo "<h1>Get Poll</h1>";
   echo "<strong>Sent to api.crowdsignal.com:</strong>";
   echo "<pre>: ";
   echo htmlspecialchars( print_r( json_decode( $curl_data ), 1 ) );
   echo "</pre>";
   echo "<strong>Received from api.crowdsignal.com:</strong>";
   $data = send_json_query( $curl_data );
   echo "<pre>: ";
   echo htmlspecialchars( print_r( $data, 1 ) );
   echo "</pre>";
   echo "<h2>First Five Poll Votes</h2>";
   $curl_data = json_encode(
     array(
       "pdRequest" => array(
         "partnerGUID" => $_POST[ 'partnerGUID' ],
         "userCode" => $_POST[ 'usercode' ],
         "demands" => array(
           "demand" => array(
             "poll" => array(
             ),
             "id" => "GetPollVotes",
             "list" => array(
               "start" => 0,
               "end" => 5,
               "id" => (int)$_POST[ 'id' ]
             ),
           )
         )
       )
     )
   );
   $data = send_json_query( $curl_data );
   echo "<pre>: ";
   echo htmlspecialchars( print_r( $data, 1 ) );
   echo "</pre>";
   break;

case "list":
   /*
    * List all the polls in the account.
    */
   pd_api_menu( $_POST[ 'partnerGUID' ], $_POST[ 'usercode' ] );
   $curl_data = json_encode(
     array(
         "pdRequest" => array(
           "partnerGUID" => $_POST[ 'partnerGUID' ],
           "userCode" => $_POST[ 'usercode' ],
           "demands" => array(
             "demand" => array(
               "id" => "GetPolls"
             )
           )
         )
       )
     );
   echo "<h1>List Polls</h1>";
   echo "<strong>Sent to api.crowdsignal.com:</strong>";
   echo "<pre>: ";
   echo htmlspecialchars( print_r( json_decode( $curl_data ), 1 ) );
   echo "</pre>";
   $data = send_json_query( $curl_data );
   echo "<strong>Received from api.crowdsignal.com:</strong>";
   echo "<pre>: ";
   echo htmlspecialchars( print_r( $data, 1 ) );
   echo "</pre>";
   if ( is_object( $data ) && isset( $data->pdResponse->demands->demand ) ) {
    echo "<strong>Your Polls</strong><ol>";
    foreach( $data->pdResponse->demands->demand[ 0 ]->polls->poll  as $poll ) {
     echo "<li><a href='https://app.crowdsignal.com/polls/" . (int)$poll->id . "/edit/'>" . htmlspecialchars( $poll->content ) . "</a> ";
     ?>
     <form method='post' action=''>
     <input name='action' type='hidden' value='getpoll' />
     <input name='partnerGUID' type='hidden' value='<?php echo htmlspecialchars( $_POST[ 'partnerGUID' ] ); ?>' />
     <input name='usercode' type='hidden' value='<?php echo htmlspecialchars( $_POST[ 'usercode' ] ); ?>' />
     <input name='id' type='hidden' value='<?php echo (int)$poll->id ?>' />
     <input type='submit' value='Get Poll <?php echo (int)$poll->id ?>' />
     </form>
     </li>
     <?php
    }
    echo "</ol>";
   }
   break;
case "usercode":
   /*
    * Get a userCode for the account.
    */
   $curl_data = json_encode(
   array(
     "pdAccess" => array(
       "partnerGUID" => $_POST['partnerGUID'],
       "partnerUserID" => "0",
       "demands" => array(
         "demand" => array(
           "id" => "GetUserCode"
         )
       )
     )
   ) );
   echo "<h1>pdAccess</h1>";
   echo "<strong>Sent to api.crowdsignal.com:</strong>";
   echo "<pre>: ";
   echo htmlspecialchars( print_r( json_decode( $curl_data ), 1 ) );
   echo "</pre>";
   $data = send_json_query( $curl_data );
   echo "<strong>Received from api.crowdsignal.com:</strong>";
   echo "<pre>: ";
   echo htmlspecialchars( print_r( $data, 1 ) );
   echo "</pre>";
   if ( false == isset( $data->pdResponse->userCode ) ) {
     pd_api_login_form( $_POST[ 'partnerGUID' ] );
     die( "Error connecting to your Crowdsignal account: <pre>" . htmlspecialchars( print_r( $data, 1 ) ) . "</pre>" );
   }
   $usercode = $data->pdResponse->userCode;
   echo "Your new usercode is: " . htmlspecialchars( $usercode ) . ".<br />";
   pd_api_menu( $_POST[ 'partnerGUID' ], $usercode );
   break;
 case "create":
   /*
    * Create an example poll.
    */
   pd_api_menu( $_POST[ 'partnerGUID' ], $_POST[ 'usercode' ] );
   $curl_data = json_encode(
   array(
     "pdRequest" => array(
       "partnerGUID" => $_POST[ 'partnerGUID' ],
       "userCode" => $_POST[ 'usercode' ],
       "demands" => array(
         "demand" => array(
           "poll" => array(
             "question" => "Do you like polls? " . date( 'Y-m-d H:i:s' ),
             "multipleChoice" => "no",
             "randomiseAnswers" => "yes",
             "otherAnswer" => array(
               "content" => "yes"
             ),
             "resultsType" => "percent",
             "blockRepeatVotersType" => "cookie",
             "blockExpiration" => "7257600",
             "comments" => array(
               "content" => "moderate"
             ),
             "makePublic" => "yes",
             "results_order_by" => "position",
             "closePoll" => "yes",
             "closeDate" => "2025-05-30T00:42:00",
             "styleID" => "15",
             "width"   => "500px",
             "packID" => "11577",
             "folderID" => "140644",
             "languageID" => "30",
             "sharing" => "no",
             "answers" => array(
                 "answer" => array(  0 => array( "text" => "Yes" ),
                           1 => array( "text" => "No" ) )
             ),
           ),
           "id" => "CreatePoll"
          )
        )
      )
    )
  );
  echo "<h1>createPoll</h1>";
  echo "<strong>Sent to api.crowdsignal.com:</strong>";
  echo "<pre>: ";
  echo htmlspecialchars( print_r( json_decode( $curl_data ), 1 ) );
  echo "</pre>";
  $data = send_json_query( $curl_data );
  echo "<strong>Received from api.crowdsignal.com:</strong>";
  echo "<pre>: ";
  echo htmlspecialchars( print_r( $data, 1 ) );
  echo "</pre>";
  if ( isset( $data->pdResponse->demands->demand[0]->poll ) ) {
     echo "<strong>Created new <a href='https://app.crowdsignal.com/polls/" . (int)$data->pdResponse->demands->demand[0]->poll->id . "/edit/'>poll</a>.</strong>";
   } else {
     echo "<strong>Could not create new poll.</strong>";
   }
   break;
default:
   pd_api_login_form();
   break;
} 

/*
 * Send a JSON request to the Crowdsignal API and return the result as an object.
 */
function send_json_query( $curl_data ) {
   $ch = curl_init();
   curl_setopt( $ch, CURLOPT_URL, "https://api.crowdsignal.com/v1" );
   curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
   curl_setopt( $ch, CURLOPT_POST, 1 );
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt( $ch, CURLOPT_POSTFIELDS, $curl_data );
   $data = curl_exec( $ch );
   curl_close( $ch );
   return json_decode( $data );
} 
 /*
  * Login form used to get a userCode.
  */
function pd_api_login_form( $partnerGUID = '' ) {
?>
   <form method='post' action=''>
   <input name='action' type='hidden' value='usercode' />
 partnerGUID/API Key: <input type='text' name='partnerGUID' value='<?php echo htmlspecialchars( $partnerGUID ); ?>' /><br />
   <input type='submit' value='Get UserCode' /><br />
   </form>
   <?php
} 

function pd_api_menu( $partnerGUID, $usercode ) {
   pd_api_menu_item( 'list', 'List Polls', $partnerGUID, $usercode );
   pd_api_menu_item( 'create', 'Create Poll', $partnerGUID, $usercode );
}

function pd_api_menu_item( $action, $submit_action, $partnerGUID, $usercode ) {
   ?>
   <form method='post' action=''>
   <input name='action' type='hidden' value='<?php echo $action; ?>' />
   <input name='partnerGUID' type='hidden' value='<?php echo htmlspecialchars( $partnerGUID ); ?>' />
   <input name='usercode' type='hidden' value='<?php echo htmlspecialchars( $usercode ); ?>' />
   <input type='submit' value='<?php echo $submit_action; ?>' />
   </form>
   <?php
 }