php - How to start REST client from zf2 skeleton application -


in short, want create client uses http basic authentication straight skeleton of zend framework 2.

the client has authorize , send post new message.

am starting onscratch (not quite - have skeleton f2) can somee explain me need start , how initiate zend_rest_client?

edit: looked more closely on stack overflow , found similar question

now indexcontroller.php file looks this:

<?php  namespace application\controller;  use zend\mvc\controller\abstractactioncontroller; use zend\view\model\viewmodel;  use zend\http\request; use zend\http\client; use zend\stdlib\parameters;  class indexcontroller extends abstractactioncontroller {     public function indexaction()     {          $request = new request();          $request->getheaders()->addheaders(array(            'content-type' => 'application/x-www-form-urlencoded; charset=utf-8'          ));          $someurl="http://apiurl/public_timeline.json";          $request->seturi($someurl);          $request->setmethod('get');          $request->setpost(new parameters(array('page' => 1)));           $client = new client();          $response = $client->dispatch($request);          $data = json_decode($response->getbody(), true);           print_r($data);           return new viewmodel();     } } 

the above code works, want extend module support methods require authentication. how so?

zend's http client supports basic http authentication , can authenticate client before dispatching request this:

$client = new client(); $client->setauth('username', 'password'); $response = $client->dispatch($request); 

for more advanced authentication mechanisms oauth2, recommend utilizing 3rd party oauth client library instead of writing own. there lot of open source , well-written oauth2 client libraries exists on github. (for example)

when grab access/refresh token (or client id & secret key) remote provider, set information in request object this:

$request->getheaders()->addheaders(array(     'accept' => 'application/json',     'authorization' => 'bearer 1234567890abcdefghxxxx', // access token ));  $client = new client(); $response = $client->dispatch($request); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -