Working example Zend_Rest_Controller with Zend_Rest_Client? - php

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.

Related

OAuth 2.0 and php slim

I am following this guide (http://www.lornajane.net/posts/2013/oauth-middleware-for-slim) to setup oAuth2 with php SLIM.
I dont't understand this part:
$auth = new \Service\Mysql\AuthService($this->mysql, $this->config);
$validated_user_id = $auth->verifyOAuth($authHeader);
$this->app->user_id = $validated_user_id;
Where can I take the class \Service\Mysql\AuthService and what is the variable config ?
Otherwise is there another guide with more details also without direct SLIM implementation ?
Thanks
That's the service class that will actually do the loading and storing of user details.
Then you should put your own class.
This could be useful: https://github.com/alexbilbie/oauth2-example-resource-server

Zend Framework 2 JSON-RPC Invalid Request

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.

accessing flash messenger not from a controller

I'm writing a PHP application using zend framework 2.2.2.
I would like to know how to be able to use the FlashMessanger using zend framework 2.
now I know that it's possible to fetch the Flash Messanger using zf1 using the following code:
$this->messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('flashMessenger');
how it is possible to fetch the flash messenger using zf2 ?
the thing is that I want to the flash messenger to be available in my own utility class
so I don't have the controller available to fetch the messenger from there.
any ideas?
thanks
The FlashMessenger is a ControllerPlugin Zend\Mvc\Controller\Plugin\FlashMessenger and that's where it makes the most sense of using it. Injecting the Messenger into your "UtilityClasses" to me sounds like a somewhat bad idea, since this would make the response-checking so much more complicated and the controllers quite more bloated. So take that in mind.
However it is possible to get the FlashMessenger into any class you want. The only catch is, all the classes you want the FM to be available at, have to be called by the ServiceManager. Your ServiceFactories then would look like this:
// Module#getServiceConfig()
return array('factories' => array(
'MyServiceClass' => function($serviceLocator) {
return new MyService(
$serviceLocator->get('controllerpluginmanager')->get('flashmessenger')
);
}
));
Of course you could re-write it to use setter-injection or even lazy-getters in your ServiceClass if you wish you inject the full ServiceLocator (which isn't advised).

Creating a web service in PHP

I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from another language like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
I would suggest you go for Yii it is worth of learning. You can easily establish it in this.
Web Service. Yii provides CWebService and CWebServiceAction to simplify the work of implementing Web service in a Web application. Web service relies on SOAP as its foundation layer of the communication protocol stack.
Easiest way in PHP is to use GET/POST as data-in and echo as data-out.
Here's a sample:
<?php if(empty($_GET['method'])) die('no method specified');
switch($_GET['method']){
case 'add': {
if(empty($_GET['a']) || empty($_GET['b'])) die("Please provide two numbers. ");
if(!is_numeric($_GET['a']) || !is_numeric($_GET['b'])) die("Those aren't numbers, please provide numbers. ");
die(''.($_GET['a']+$_GET['b']));
break;
}
}
Save this as test.php and go to http://localhost/test.php?method=add&a=2&b=3 (or wherever your webserver is) and it should say 5.
PHP does have native support for a SOAP server ( The SoapServer class manual shows it) and I've found it pretty simple to use.
Creating a REST style API is pretty easy if you use a framework. I don't want to get into a debate about which framework is better but CakePHP also supports output as XML and I'm pretty sure others will as well.
If you're coming from a Microsoft background just be careful about thinking about "datasets". They are a very specific Microsoft thing and have been a curse of mine in the past. It's probably not going to be an issue for you, but you may want to just see the differences between Microsoft and open implementations.
And of course PHP has a native json_encode() function.
You can check out this nice RESTful server written for Codeigniter, RESTful server.
It does support XML, JSON, etc. responses, so I think this is your library.
There is even a nice tutorial for this on the Tutsplus network -
Working with RESTful Services in CodeIgniter
You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services
You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to build the webservices.
You can also use plain PHP like disscussed in this example

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.

Categories