davidwnek/survey_monkey_api_client_bundle
Symfony bundle integrating a SurveyMonkey API v3 client with OAuth setup. Configure client ID/secret and redirect URL via config/parameters, enable the bundle, and optionally use the built-in controller to handle the auth code callback.
Install the Bundle
composer require davidwnek/survey-monkey-api-client-bundle dev-master
Note: Use dev-master as the package is outdated (last release in 2018). Ensure your Laravel/Symfony project is compatible (Symfony 3.x+).
Enable the Bundle
Add to config/app.php (Laravel) or AppKernel.php (Symfony):
davidwnek\SurveyMonkeyApiClientBundle\SurveyMonkeyApiClientBundle::class,
Configure Credentials
Add to .env (Laravel) or parameters.yml:
SM_CLIENT_ID=your_client_id
SM_CLIENT_SECRET=your_client_secret
SM_REDIRECT_URL=http://your-app.com/survey_monkey/code
Generate credentials via SurveyMonkey Developer Portal.
Set Up Routing
Add to routes/web.php (Laravel) or routing.yml (Symfony):
// Laravel
Route::get('/survey_monkey/code', [\davidwnek\SurveyMonkeyApiClientBundle\Controller\AuthenticationController::class, 'authenticate']);
First API Call Inject the client into a service/controller:
use davidwnek\SurveyMonkeyApiClientBundle\Service\SurveyMonkeyApiClient;
public function __construct(SurveyMonkeyApiClient $client) {
$this->client = $client;
}
// Example: Fetch surveys
$surveys = $this->client->get('/surveys');
OAuth Flow
return redirect($this->client->getAuthUrl());
/survey_monkey/code) to exchange the code for an access token. The bundle provides a built-in AuthenticationController for this.API Resource Management
$survey = $this->client->post('/surveys', ['title' => 'Customer Feedback']);
$survey->questions = $this->client->get("/surveys/{$survey->id}/questions");
Pages and PagesQuestions endpoints (added in v1.1.0).
$page = $this->client->post("/surveys/{$survey->id}/pages", ['title' => 'Page 1']);
$question = $this->client->post("/pages/{$page->id}/questions", ['title' => 'Question 1']);
Error Handling Wrap API calls in a try-catch block to handle SurveyMonkey API errors (e.g., 401 Unauthorized, 404 Not Found):
try {
$response = $this->client->get('/surveys');
} catch (\GuzzleHttp\Exception\RequestException $e) {
$error = json_decode($e->getResponse()->getBody(), true);
// Log or handle error (e.g., redirect to login if 401)
}
Token Refresh Store the access token and refresh token in the session or database. Refresh the token when it expires:
$this->client->setAccessToken($refreshToken);
$newAccessToken = $this->client->refreshAccessToken();
Laravel-Specific:
Http facade or GuzzleHttp directly if you need more control over requests.encrypter or sanctum for secure access.namespace App\Services;
use davidwnek\SurveyMonkeyApiClientBundle\Service\SurveyMonkeyApiClient;
use Illuminate\Support\Facades\Cache;
class SurveyMonkeyService {
public function __construct(SurveyMonkeyApiClient $client) {
$this->client = $client;
}
public function getSurvey($id) {
return Cache::remember("survey_{$id}", now()->addHours(1), function() use ($id) {
return $this->client->get("/surveys/{$id}");
});
}
}
Symfony-Specific:
SurveyMonkeyApiClient into services.security component or a custom ParameterBag.Testing:
SurveyMonkeyApiClient in PHPUnit tests:
$mockClient = $this->createMock(SurveyMonkeyApiClient::class);
$mockClient->method('get')->willReturn(['survey' => 'data']);
$this->app->instance(SurveyMonkeyApiClient::class, $mockClient);
Outdated Package:
guzzlehttp/guzzle, symfony/http-foundation).OAuth Flow Complexity:
Session or Symfony’s Session component to manage the OAuth state.Token Management:
access_token and refresh_token after the OAuth callback.auth:sanctum or Symfony’s security component to manage tokens.Rate Limiting:
$client = new SurveyMonkeyApiClient();
$client->getClient()->getEmitter()->attach(
new \GuzzleHttp\Middleware::retry(
new \GuzzleHttp\RetryMiddleware(),
$backoff = 100
)
);
Environment-Specific Redirect URLs:
redirect_url must match exactly what’s configured in SurveyMonkey’s developer portal. Use environment-specific values (e.g., localhost for dev, yourdomain.com for prod)..env or Symfony’s parameters.yml to manage this:
# .env
SM_REDIRECT_URL=${APP_ENV_LOCAL}http://localhost:8000/survey_monkey/code:${APP_ENV_PROD}https://yourdomain.com/survey_monkey/code
Enable Guzzle Debugging:
Add this to your SurveyMonkeyApiClient configuration to log requests/responses:
$client = new SurveyMonkeyApiClient();
$client->getClient()->getConfig()->setDebug(true);
$client->getClient()->getEmitter()->attach(
new \GuzzleHttp\HandlerStack()
);
SurveyMonkey API Errors: SurveyMonkey returns errors in the format:
{
"error": {
"code": "invalid_request",
"message": "Invalid parameter: title"
}
}
Tip: Parse the error response to provide user-friendly feedback.
CORS Issues: If you encounter CORS errors when calling the API from a frontend, ensure:
redirect_url.Custom Authentication Flow:
Extend the AuthenticationController to:
namespace App\Controller;
use davidwnek\SurveyMonkeyApiClientBundle\Controller\AuthenticationController as BaseController;
class CustomAuthenticationController extends BaseController {
protected function getSuccessRedirectRoute() {
return 'dashboard';
}
protected function getFailureRedirectRoute() {
return 'login';
}
}
Add Custom Endpoints:
Extend the SurveyMonkeyApiClient to support additional SurveyMonkey API endpoints:
How can I help you explore Laravel packages today?