Zend Framework 2 JSON-RPC Invalid Request - php

i'm running into some problems trying to implement JSON-RPC server under the Zend Framework 2 using the official documintation
i have created the calculator class under my application/model/calculator.php
but in the application/controller/indexController.php i have unsuccessfully being able to handel the server request:
public function indexAction(){
$server = new \Zend\Json\Server\Server();
// Indicate what functionality is available:
$server->setClass('Application\Model\Calculator');
// Handle the request:
$server->handle();
$view = new ViewModel();
return $view;
}
and getting the following error:
{"error":{"code":-32600,"message":"Invalid Request","data":null},"id":null}
needless to say i have not found any good tutorials on the web of implementing JSON-RPC to zend framework2.

i used zf1 for that and considering using zf2.
the only advice is to check request, maybe u not using the named arguments.
whatever, just debug the zf2 source cause why not.

Related

How do I make a guzzle response into a response to a request from a client?

I have an application running on webserver A. I have a second application running on webserver B. Both webservers require a login. What I need to do is have a request to webserver A pass through to webserver B and return a file to the client without having the client login to Webserver B. (In other words, webserver B will be invisible to the client and I will take care of the auth credentials with my request to B from A). The code below is built on a laravel framework, but I don't believe the answer needs to be laravel specific.
The code works but it is only returning the HEAD information of the file to the calling client. Not the file itself.
Any help will be greatly appreciated!
Controller:
public function getAudioFile(Request $request)
{
//This is the id we are looking to pull
$uid = $request->uniqueid;
$audioServices = new AudioServices();
return $audioServices->getWavFile($uid);
}
Service:
public function getWavFile(String $uniqueId)
{
$client = new GuzzleHttp\Client(['verify' => false]);
return $client->request('GET', $this->connectString.$uniqueId, ['auth' => ['username', 'password']]);
}
As mentioned by bishop you can use sink option from Guzzle to stream the response of a Guzzle request.
You can pass that stream to a response from your controller. I'm not sure if Laravel has built-in stream support, but the underlying symfony httpfoundation components do. An example of it's usage can be found in this tutorial.
If you prefer not to use the sink option from Guzzle you can also use the response itself as that implements PSR-7 stream objects.

rendering symfony/form in non-symfony application

I'm trying to rewrite a "custom framework" application to the Symfony, but I can not do everything at once, so I've divided the process into steps.
From important notes - I've already implemented the symfony/templating component and the symfony/twig-bridge component.
That's how I want to output the form in the template:
<?php echo $view['form']->form($form) ?>
As I'm doing so the following error is thrown:
Symfony\Component\Form\Exception\LogicException
No block "form" found while rendering the form.
/var/www/html/vendor/symfony/form/FormRenderer.php on line 98
To render the templates I'm using the DelegatingEngine which uses the PhpEngine and the TwigEngine.
Setting up the Twig with the \Symfony\Bridge\Twig\Extension\FormExtension is well documented, but what I'm missing is the php setup. This is how I'm doing this:
new \Symfony\Component\Form\Extension\Templating\TemplatingExtension($phpEngine, $this->csrfManager());
Could you point me what am I missing or what's wrong with my setup?
I think the simplest way would have been to install the Symfony 3.3 standard edition next to your app (pending the release of Symfony Flex).
After this, find a way to use the router of Symfony with the router of your application.
So you could have the full Symfony framework, create your form type in it and let Symfony render it :
With an ajax call
With a new Symfony Kernel in your legacy app
I've found the answer:
I was using the wrong FormRendererEngineInterface. Instead of relying on the \Symfony\Component\Form\Extension\Templating\TemplatingExtension class I've registered the form helper by myself:
$phpEngine = new PhpEngine(new TemplateNameParser(), new FilesystemLoader(realpath(__DIR__.'/../Template').'/%name%'));
$twigEngine = new TwigEngine($this->twig(), new TemplateNameParser());
$this->TemplateEngine = new DelegatingEngine(array(
$phpEngine,
$twigEngine,
));
$phpEngine->addHelpers(array(
new FormHelper(new FormRenderer($this->twigFormRendererEngine())),
));
As you can see in the TemplatingEngine:
public function __construct(PhpEngine $engine, CsrfTokenManagerInterface $csrfTokenManager = null, array $defaultThemes = array())
{
$engine->addHelpers(array(
new FormHelper(new FormRenderer(new TemplatingRendererEngine($engine, $defaultThemes), $csrfTokenManager)),
));
}
It relies on the TemplatingRendererEngine while I need the TwigRendererEngine instance, as the form templates are the twig files.
Correct me if my explanation is wrong, but the solution is working.

How to generate wsdl

I am working for the first time on SOAP. After going through various tutorials I have been able to create a small and simple soap server which offers two functions. Although I have been able to create the SOAP service and test it with a client I am not being able to understand WSDL even after going through the tutorials e.g. at http://www.w3schools.com/wsdl/wsdl_binding.asp .
So how can I make this SOAP "wsdl soap" ?? Is there anyway way to autogenerate it in PHP? I don't mind writting it down manually provided I know what to write. Where do I put what code to make it "wsdl" ??
function geolocate(double $lat, double $lng)
{
$contents = file_get_contents('http://api.geonames.org/findNearby?lat='.$lat.'&lng='.$lng.'&username=imranomar');
return $contents;
}
function describe(long $geonameid)
{
return file_get_contents('http://ws.geonames.org/get?geonameId='.$geonameid.'&style=full');
}
$server = new SoapServer(null, array('uri' => "urn://localhost/firstmobile?wsdl"));
$server->addFunction('geolocate');
$server->addFunction('describe');
$server->handle();
Note: I cannot be using any framwork
I suggest you use Zend_Soap_Server as it simplify webservices developpement in PHP.
It is possible to auto generate SOAP WSDL using Zend AutoDiscovery

Read HTTP headers in Controller (Zend Framework)

Long story short:
I'm building a skeleton application for Zend Framework and I got to the part where I need to setup the api module. I'm using Zend_Rest_Controller for this job. All is ok up to this part where I need to get the HTTP headers in a controller to verify the api key.
On various tutorials I've read on the web the thing is done via a front controller plugin, but I need it to be more "plug and play" than that (checking each time the config of the application, deciding which module is the api and so on).
I tried what seemed most obvious $this->getRequest()->getHeaders() but doesn't seem to work, at least not for the HTTP headers where I'll be seding my api key. Neither the reponse object.
Can anyone help me with this one?
I found a way of doing this after all :)
On the preDispatch() method in your controller you can do the following:
public function preDispatch()
{
$request = new Zend_Controller_Request_Http();
$key = $request->getHeader('x-apikey');
}
It seems that Zend_Controller_Request_Http object gives you acces to the headers. More info on the Zend_Controller_Request_Http you can find here
As Bogdan said, you can find that information in the Zend_Controller_Request_HTTP class. It can be found in the controller itself by doing :
$this -> getFrontController() -> getRequest() -> getHeader('Content-Type');
Unfortunatly, you can't access all headers at once but what ZF does is just use apache_request_headers() function if available on the server to get them.

Working example Zend_Rest_Controller with Zend_Rest_Client?

I am trying to write a short Rest Service with Zend Framework. But the documentation is not the best at this Part.
I have an ApiController extended Zend_Rest_Controller with all needed abstract methods. My goal is to get Post data and return something.
My client looks like this:
public function indexAction()
{
$url = 'http://localhost/url/public/api';
$client = new Zend_Rest_Client();
$client->setUri($url);
$client->url = 'http://www.google.de';
$result = $client->post();
}
but the provided "$client->url" is not inside the post array on Server side. Does I have to use Zend Rest Server inside the postAction on my ApiController?
If someone has an example how to send and to get the data with Zend Rest, that would be great.
Maybe this tutorial Create RESTful Applications Using The Zend Framework can help.
Have you tried setting it to access 127.0.0.1 rather than localhost? I know this can cause issues sometimes.

Categories