delete with Zend_Rest_Client - php

I'm trying to delete a resource using ZF1 rest client
$this->restClient = new Zend_Rest_Client('https://myurl.com');
$response = $this->restClient->delete('/service/'.$this->uuid.'.json?api_key='.$this->apikey);
but I get an error:
Path "/service/v-2149d050-c64b-0131-33b0-1231390c0c78.json?api_key=a-9a136a00-b340-0131-2662-1231390c0c78" is not a valid HTTP path
the web service documentation simply says to use
DELETE https://myurl.com/service/YOUR_UUID.json?api_key=YOUR_API_KEY
any idea on how to use this class?
thanks

DELETE https://myurl.com/service/YOUR_UUID.json?api_key=YOUR_API_KEY
That is not the path only, but a full URI. It breaks down to:
Path: service/YOUR_UUID.json
Query-Info: api_key=YOUR_API_KEY
For Zend rest client, you need to call one function per each parameter, and a parameter can not be named as a standard HTTP verb:
$client = new Zend_Rest_Client('https://exeample.com');
$client->api_key(YOUR_API_KEY);
$response = $client->restClient->delete('/service/'.$this->uuid.'.json);
For more information please see the Request Arguments section in the vendor documentation on how to pass arguments with your request.

Related

Difference between match parameter and RequestContext in Symfony Router UrlMatcher

I don't understand the semantics of the Symfony Routing Component's API.
From the first code example on the Routing Component documentation page:
$context = new RequestContext('/');
$matcher = new UrlMatcher($routes, $context);
$parameters = $matcher->match('/foo');
Why is the hostname and HTTP method passed in via $context and the path via a parameter to match()? Or is it? There is also a path parameter in the RequestContext constructor.
One gets the impression match() is supposed to be called multiple times with different paths within one request, which I can't imagine would ever happen.
After integrating the Routing Component into my application, I now have a hunch why
it was done like that.
Most of the properties of the RequestContext - method, request body, get parameters - can be used without modification, but depending on the desired path structure and server configuration (rewrite rules, etc.) there are multiple ways in which the path needs to be preprocessed.
This doesn't explain why the path is passed to the match() function and the request object is passed to the constructor, but it does explain why they are passed in separately.

Getting the domain dynamically in a Symfony Console Command

I am encountering precisely this problem:
Symfony Docs - How to Generate URLs and Send Emails from the Console
Our email templates are being filled with "localhost" instead of "my.real-domain.name". When constructing links to the application using twig's "url('some/path')".
However, where Symfony is usually "one installation per domain", our application is designed so a single instance can handle multiple domains. It constructs the necessary configuration through various configuration channels, with each customer being one channel.
Thus I would like to avoid configuring "router.request_context.host" and others for every single customer channel.
So I would like to grab the domain to be used from a "--domain" console parameter that we give to every console command instad.
But instead of doing it in every single command, I would need to do this in one central location that grabs the domain and configures "router.request_context" dynamically according to the console parameter.
Is there any way I can do that?
You can register a listener on the event dispatcher which listens to the ConsoleEvents::COMMAND and configure it further before the run method on the command is called.
For example, you can use a setter or change the input on the ConsoleCommandEvent instance.
https://symfony.com/doc/3.4/components/console/events.html
I'm not sure this is an ideal solution, but this is what I could think of at the moment.
You can tell to the router which domain use when generating an absolute URL, as example:
$domain = $input->getArgument('domain');
$context = $this->getContainer()->get('router')->getContext();
$context->setHost($domain);
$route = $this->getContainer()->get('router')->generate('welcome_page', $params, UrlGeneratorInterface::ABSOLUTE_URL);
$output->writeln('Use the following url to show the welcome page for the provided domain');
$output->writeln(sprintf('<info>%s</info>', $route));
Hope this help

How to generate absolute assets url for non-http request

Since Symfony-2.7 the asset's method 3rd argument (a boolean that indicates whether to generate an absolute url) was deprecated.
From sources:
#trigger_error('Generating absolute URLs with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0. Please use absolute_url() instead.', E_USER_DEPRECATED);
So the official way to generate it in a Symfony-3.0 compatible way became:
{{ absolute_url(asset('some/asset.css')) }}
The problem though is that the HttpFoundationExtension that provides the absolute_url relies on the "request stack" which is empty in case of non-http request.
So it returns the original path without any transformations:
if (!$request = $this->requestStack->getMasterRequest()) {
return $path;
}
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php#L59
So the question is - how to generate a full url in a CLI-based application?
An important note: http://symfony.com/doc/current/cookbook/console/sending_emails.html this advice is not actual when you use absolute_url helper.
Try pushing dummy request object to request_stack service, also configure request context in parameters.yml as said in sending_emails.html
$request = new Request();
// if you use locales
$request->setLocale("en");
// to be able to render twig asset absolute paths we have to inject dummy request
$container->enterScope('request');
$container->set('request', $request, 'request');
$container->get('request_stack')->push($request);

Get requested service in Restler iAuthenticate implementation

I'm using Restler to develop a REST api and I need to get the requested service from the iAuthenticate implementation.
So far I have managed to get here:
$m = preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI']);
and $_SERVER['REQUEST_URI'] has this form: /somedir/apiclass/requestedservice?...
I've tried my regex here: http://www.spaweditor.com/scripts/regex/index.php with my actual uri
and it works perfectly. When I try to parse the request url inside my iAuthenticate implementation it just don't work.
Does anybody know how to enable regex within restler iAuthenticate implementation? How can I display errors in Restler instead of a blank page?
Thanks!
[EDIT]
I wasn't passing the variable to store the match object, preg_match recieves a third argument to store it and just returns a boolean. Case closed.
I finally solved it like this:
preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI'], $mo);
$api = $mo['api'];
$service = $mo['service'];

Troubles with Zend Rest Client when trying to call a Web Service developed in ASP and sending a parameter as array

I have this call to a Web Service that is developed with ASP:
$endpoint = Zend_Registry::get('config')->endpoint->services->myService;
$client = new Zend_Rest_Client($endpoint);
$client->userId($adminUserId);
$client->otherIds($otherIds);
$result = $client->get();
But when I try to call the service the parameter 'otherIds' is not been taken by the WS.
That is because the first called function apparently (as I grok from the source code) is chosen for the "method" parameter. The Zend REST server seems to take this format. I suggest for other servers to feed a dummy method to the client, so the first call should be
$client->Dummy();
After that, the arguments are set.

Categories