The library is intended to make integrating with SurveyGizmo easier and quicker than using the API directly. The following objects are supported via this library and are all namespaced under SurveyGizmo (e.g. \SurveyGizmo\Resources\Survey).
- PHP 5.3+
- cURL
- Active SurveyGizmo Account
This library is now available in packagist, and you can include surveygizmo/surveygizmo-api in your composer configuration files to autoload it:
$ composer require surveygizmo/surveygizmo-api
Using version ^1.0 for surveygizmo/surveygizmo-api
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing surveygizmo/surveygizmo-api (v1.0.3-stable): Loading from cache
Writing lock file
Generating autoload files
- Download the library and add it to your project.
- Include the SurveyGizmoAutoLoader.php file, replacing
<LIBRARY_PATH>
with the appropriate path.
require_once "<LIBRARY_PATH>/SurveyGizmoAutoLoader.php";
- If you are not using our US data center, you will need to choose the appropriate region (US, CA, or EU). If you are not sure if you are using the US, EU, or CA API, see: https://apihelp.surveygizmo.com/help/us-eu-or-ca-api
try {
\SurveyGizmo\SurveyGizmoAPI::setRegion('EU');
} catch (Exception $e) {
die('Region not available');
}
- Authenticate using your SurveyGizmo API Key and Secret.
try {
\SurveyGizmo\SurveyGizmoAPI::auth("<YOUR API_KEY>", "<YOUR API_SECRET>");
} catch (Exception $e) {
die("Error Authenticating");
}
- If needed, configure rate limiting.
//set max retries of requests to 10, when request is rate limited it will be retried after 5 seconds.
\SurveyGizmo\ApiRequest::setRepeatRateLimitedRequest(10);
- If needed, configure the request timeout duration (in seconds, defaults to 35 if not specified)
//this is the maximum wait period before aborting requests, you may need to
//increase this from the default of 35 seconds if working with oversize surveys
\SurveyGizmo\ApiRequest::setRequestTimeout(60);
Please refer to the Samples folder for more thorough example use cases.
To use these samples, copy the example file and then supply your own credentials:
$ cd Samples
$ cp .credentials.example .credentials
$ vi .credentials # then supply your credentials accordingly
$ php new_survey.php # run once prior to running manipulate_survey.php
$ php manipulate_survey.php
- Survey
- Responses
- Questions
- Pages
- Statistics
- Reports
- Campaigns
- EmailMessages
- Account
- Users
- Teams
- Contacts
- ContactLists
- Contacts
This Library uses the version 5 SurveyGizmo API, please refer to our API Documentation for more information.
<OBJECT>::fetch(<FILTERS>,<OPTIONS>);
Returns an array of objects based on filter and paging options.
<OBJECT>::get($id);
Returns a single object based on id
<OBJECT>->save();
Saves a newly created or updated instance of an object
<OBJECT>->delete();
Deletes an instance of an object
See filter and paging below.
$surveys = \SurveyGizmo\Resources\Survey::fetch(<FILTER>,<OPTIONS>);
$survey_id = <SURVEY_ID>;
$survey = \SurveyGizmo\Resources\Survey::get(survey_id);
$survey->title = "TEST UPDATE FROM API LIBRARY";
$survey->save();
$survey = new \SurveyGizmo\Resources\Survey();
$survey->title = "NEW SURVEY";
$results = $survey->save();
$survey = $survey->delete();
The Survey object provides a few help functions to easily access related collections and objects.
//get questions
$survey->getQuestions(<FILTER>,<PAGE>);
$survey->getQuestion($question_id);
//get responses
$survey->getResponses(<FILTER>,<PAGE>);
$survey->getResponse($id);
//get reports
$survey->getReports(<FILTER>,<PAGE>);
$survey->getReport($id);
//get statistics
$survey->getStatistics();
$survey->getStatisticsByID($question_id);
//get campaigns
$survey->getCampaigns();
$survey->getCampaign($id);
//get email messages
$survey->getCampaign($id)->getEmailMessages();
$survey->getCampaign($id)->getEmailMessage($email_id);
To access the questions on a survey you'll need an instance of a \SurveyGizmo\Resources\Survey object.
$questions = $survey->getQuestions();
$question = $survey->getQuestion(<QUESTION question_id>);
$question->title->English = "LIBRARY TEST";
$ret = $question->save();
To access the responses for a survey you'll need an instance of a \SurveyGizmo\Resources\Survey object. See filter and paging below.
$responses = $survey->getResponses(<FILTER>,<OPTIONS>);
$responses = $survey->getResponse(<RESPONSE_ID);
$response->survey_data[$question_id]['answer'] = 'YES';
$ret = $response->save();
All fetch methods take both optional $filter and $options arguments.
$filter = new \SurveyGizmo\Helpers\Filter();
$filter_item = new \SurveyGizmo\Helpers\FilterItem();
$filter_item->setField('title');
$filter_item->setOperator('=');
$filter_item->setCondition('TEST from API');
$filter->addFilterItem($filter_item);
$surveys = \SurveyGizmo\Resources\Survey::fetch($filter);
Sometimes you will need to page through collections of objects. To accommodate this use the optional $options argument on any fetch method;
$options = array( 'page' => 3, 'limit' => 100 );
$surveys = \SurveyGizmo\Resources\Survey::fetch($filter,$options);
In the case of an error we will return the following responses and status codes:
Method not implemented (404)
Method not supported (405)
Not Authorized (401)
To perform a API call without going through a specific resource class, use \SurveyGizmo\ApiRequest::call.
$response = \SurveyGizmo\ApiRequest::call('contactlist', null, null, null);
Unit tests are included under the /Tests
directory. They can be run by calling PHPUnit within the Tests folder:
$ phpunit
The library was developed and is maintained by the SurveyGizmo Development Team.
This project is licensed under the terms of the MIT license.