miholeus

curl protocol

Jan 16th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @namespace
  4.  */
  5. namespace Application\Service\Api\Protocol;
  6. /**
  7.  * Api Handler BasicProtocol
  8.  *
  9.  * @author miholeus
  10.  */
  11. class Curl extends AbstractProtocol
  12. {
  13.     protected $errors;
  14.     public function execute($action, $params)
  15.     {
  16.         $config = array(
  17.             'adapter'   => 'Zend_Http_Client_Adapter_Curl',
  18.             'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
  19.         );
  20.         $options = $this->getHandler()->getOptions();
  21.  
  22.         $client = new \Zend_Http_Client($options['uri'], $config);
  23.         $client->setParameterGet(array_merge(array(
  24.             'request' => $action,
  25.             'format' => 'json'
  26.         ), $params));
  27.  
  28.         $client->request();
  29.  
  30.         $response = $client->getLastResponse();
  31.        
  32.         $this->service = new Curl\Service($this);
  33.        
  34.         $body = \Zend_Json_Decoder::decode($response->getBody());
  35.         if(is_array($body)) {
  36.             if(isset($body['response']['error'])) {
  37.                 $this->service->addErrors($body['response']['error']);
  38.             }
  39.             if(isset($body['response']['data'])) {
  40.                 return $body['response']['data'];
  41.             }
  42.         }
  43.  
  44.         return false;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment