Iam new to SQS php SDK, Iam not able to override RedrivePolicy using setQueueAttributes method :(
json string is not accepted as an attribute and I cannot find any clear resources to help me.
Have a look at the below example code:
$queueUrl = "QUEUE_URL";
$client = new SqsClient([
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2012-11-05'
]);
try {
$result = $client->setQueueAttributes(array(
'Attributes' => [
'ReceiveMessageWaitTimeSeconds' => 20
],
'QueueUrl' => $queueUrl, // REQUIRED
));
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
Full code:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/php/example_code/sqs/LongPollingSetQueueAttributes.php
Related
I am trying to add FitBit OAuth to my Yii2 app. I have successfully managed to setup other APIs, such as Google and Facebook - but these are using yii\authclient\clients. Here I am trying to write my own using yii\authclient\OAuth2. Here is the code I have:
<?php
namespace app\auth\clients;
class Fitbit extends \yii\authclient\OAuth2{
public $authUrl = 'https://www.fitbit.com/oauth2/authorize';
public $tokenUrl = 'https://api.fitbit.com/oauth2/token';
public $apiBaseUrl = 'https://api.fitbit.com';
public $scope = 'profile';
protected function initUserAttributes(){
return $this->api('/1/user', 'GET');
}
protected function defaultName(){
return 'fitbit';
}
protected function defaultTitle(){
return 'Fitbit';
}
}
Here is the configuration I have:
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
'class' => 'yii\authclient\clients\Google',
'clientId' => 'ID',
'clientSecret' => 'SECRET',
],
'fitbit' => [
'class' => 'app\auth\clients\Fitbit',
'clientId' => 'ID',
'clientSecret' => 'SECRET',
],
But I am stumped, as this is the error I get:
{"errors":[{
"errorType":"invalid_request",
"message":"Authorization header required. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."
}],
"success":false}
Any help would be really appreciated. Thank you.
EDIT My current thinking is something along the lines of:
return $this->api('/1/user', 'GET', [], [
'WWW-Authenticate' => 'Authorization: Basic ' . base64_encode($this->client_id . ':' . $this->client_secret),
]);
But this still gives me the same error.
I am attempting to install APIGILITY in my app. I have followed this tutorial:
https://apigility.org/documentation/recipes/apigility-in-an-existing-zf2-application
When I attempt to access the apigility admin: www.myapp.dev/apigility I get a "The requested URL could not be matched by routing" error.
My config is as follows:
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'ZfcRbac', //Keep this at the top
'Application', //The applications main functions run from this module
//APIGILITY
'ZF\Apigility',
'ZF\Apigility\Provider',
'AssetManager',
'ZF\ApiProblem',
'ZF\MvcAuth',
'ZF\OAuth2',
'ZF\Hal',
'ZF\ContentNegotiation',
'ZF\ContentValidation',
'ZF\Rest',
'ZF\Rpc',
'ZF\Versioning',
'ZF\DevelopmentMode',
'ZF\Apigility\Admin',
'ZF\Configuration',
I have enabled developer mode.
Typically if a route exists and ZfcRbac is blocking the route, I am re-directed. In this case when the route is not accessible I get the error.
Is there a simple way to test this?
To follow up on HappyCoder's own answer, you can match all routes in zf-apigility module with
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
// Route matched
$route_name = $e->getRouteMatch()->getMatchedRouteName();
// If apigility - set correct layout
if(preg_match('/^zf-apigility/', $route_name)) {
$e->getViewModel()->setTemplate('layout/api-layout');
}
}
);
}
When doing this way - it will set appropriate layout for all apigility views, including /apiligity (welcome screen)
I solved this issue by doing the following:
The tutorial makes no mention of copying the ApiGility template to your app. You need to do this. What I did was to add the template to my application/config/module.config.php file.
return [
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/exception',
'template_map' => [
'customer/layout' => __DIR__ . '/../view/layout/customer-layout.phtml',
'api/layout' => __DIR__ . '/../view/layout/api-layout.phtml',
'layout/layout' => __DIR__ . '/../view/layout/admin-layout.phtml',
In the Application module I check routing and switch the template accordingly:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
//Set the customer layout
$needle = $e->getRouteMatch()->getParam('controller');
$haystack = [
/* Customer template routes */
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('customer/layout');
}
//Apigility route
$haystack = [
'zf-apigility/ui'
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('api/layout');
}
}
);
}
To access the apigility pages, I now access via: http://www.myapp.com/apigility/ui#/
Hope this helps someone...
I have a problem implemented the ZfcRbac. At the moment the Rbac works and i enter a page on a certain url i will receive a 403. The code i used for this is the following:
'zfc_rbac' => array(
'guards' => array(
'ZfcRbac\Guard\RouteGuard' => array(
'relation*' => array('relation'),
),
),
'role_provider' => array(
'ZfcRbac\Role\InMemoryRoleProvider' => array(
'relation' => array(
'permissions' => array('view', 'edit', 'delete'),
),
),
),
),
The problem occurs when i try to let it talk with the Zend Navigation helper. Due some odd reason the navigation keeps telling me it can access the page. When i click it it seems you can't. Here is some code to check it
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$sharedEvents = $application->getEventManager()->getSharedManager();
$authorization = $serviceManager->get('ZfcRbac\Service\AuthorizationService');
$sharedEvents->attach(
'Zend\View\Helper\Navigation\AbstractHelper', 'isAllowed', function (EventInterface $e) use ($authorization)
{
$page = $e->getParam('page');
$permission = $page->getPermission();
return $authorization->isGranted($permission);
}
);
}
And the view helper will be called like:
<?=$this->navigation('navigation')->menu()->setPartial(array('sidebar.phtml', 'css'=>'nav navbar-nav'))->render();?>
Hope someone knows the answer on this one.
Thanks in advance.
In Zend2 you can do this:
<?php echo $this->currencyFormat(120, 'ZAR'); ?>
This will result in:
ZAR 120.00
However, I want to end up with:
R 120.00
How can I set the prefix to rather be the currency symbol, as apposed to the code? The following doesn't work (obviously):
<?php echo $this->currencyFormat(120, 'R'); ?>
Figured it out myself. Easy as this:
$helper->setCurrencyPattern('R #0.#');
So the complete code which allows me to control everything in one place (Module.php) is as follows:
class Module
{
public function getConfig()
{
return array(
'view_helpers' => array(
'factories' => array(
'currencyFormat' => function($sm)
{
$helper = new \Zend\I18n\View\Helper\CurrencyFormat;
$helper->setCurrencyCode("ZAR");
$helper->setLocale('us_ZA');
$helper->setCurrencyPattern('R #0.#');
return $helper;
},
)
),
);
}
}
Enjoy...
If have a Check within my Module.php and need to throw Error and Redirect to the error page.
Can someone please advice me how i can handle this?
$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use($serviceManager, $capture)
{
if($foo == null) {
throw new \Exception("
}
}
This should do the trick, although it requires you having your error templates mapped in your module.config:
$response = $e->getResponse();
$response->setStatusCode(500);
$response->sendHeaders();
//and in module.config
'view_manager' => array(
'display_exceptions' => true,
'exception_template' => 'error/index',