Example #1
0
 /**
  * Sends the data via cURL
  * @param array $params
  * @param integer $method
  * @param string $raw_body
  * @return Zend_Http_Response
  */
 private function curl($orig_url, $params = array(), $method = Request::GET, $headers = array(), $remove_unsafe_params = true, $retry_count = 0)
 {
     try {
         if (is_null($orig_url) && defined('MO_API_URL')) {
             $orig_url = MO_API_URL;
         } else {
             if (is_null($orig_url)) {
                 throw new \Exception('No url was provided and MO_API_URL is not defined');
             }
         }
         $url = $orig_url . $this->getFunc();
         if ($method == Request::DELETE || $method == Request::PUT) {
             if (isset($params['_id'])) {
                 $url .= '/' . (isset($params['_id']) ? (int) $params['_id'] : 0);
             }
         }
         // remove any routing to prevent overwritting the url
         if ($remove_unsafe_params) {
             if (isset($params['module'])) {
                 unset($params['module']);
             }
             if (isset($params['controller'])) {
                 unset($params['controller']);
             }
             if (isset($params['action'])) {
                 unset($params['action']);
             }
             if (isset($params['func'])) {
                 unset($params['func']);
             }
         }
         // gots to do this so that transfer-encoding: chunked comes through properly
         $curl_adapter = new \Zend\Http\Client\Adapter\Curl();
         // Enforce a 30 second timeout (default is unlimited)
         if ($this->getTimeout() > 0) {
             $curl_adapter->setCurlOption(CURLOPT_TIMEOUT, $this->getTimeout());
             $curl_adapter->setCurlOption(CURLOPT_CONNECTTIMEOUT, $this->getTimeout());
         }
         //$curl_adapter->setCurlOption(CURLOPT_ENCODING , "gzip");
         $curl_adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false);
         $curl_adapter->setCurlOption(CURLOPT_USERAGENT, 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10');
         $http_client = new \Zend\Http\Client();
         $http_client->setAdapter($curl_adapter);
         $http_client->setUri($url);
         $http_client->setArgSeparator('&');
         $request = new \Zend\Http\Request();
         $request->setUri($url);
         //$http_client->setCookieJar($this->getHttpCookieJar());
         // Set the token authentication
         $request->getHeaders()->addHeaderLine('Accept-Encoding', 'gzip,deflate');
         $request->getHeaders()->addHeaderLine('Content-Type', \Zend\Http\Client::ENC_URLENCODED);
         if (is_array($headers)) {
             foreach ($headers as $key => $header) {
                 $request->getHeaders()->addHeaderLine($key, $header);
             }
         }
         if ($request->getUri() === null) {
             throw new \Exception('No URI given. Param \'func\' is required.');
         }
         /* @var $response Zend_Http_Response */
         $response = false;
         if ($method == \Mojavi\Request\Request::GET) {
             if (is_array($params)) {
                 $request->getQuery()->fromArray($params);
             }
             $request->setMethod(\Zend\Http\Request::METHOD_GET);
             $response = $http_client->send($request);
         } else {
             if ($method == \Mojavi\Request\Request::POST) {
                 // If we uploaded files, we have to send them differently
                 if (count($_FILES) > 0) {
                     $request->getFiles()->fromArray($_FILES);
                     $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA);
                 } else {
                     $http_client->setEncType(\Zend\Http\Client::ENC_URLENCODED);
                 }
                 if (is_array($params)) {
                     $request->getPost()->fromArray($params);
                 }
                 $request->setMethod(\Zend\Http\Request::METHOD_POST);
                 $response = $http_client->send($request);
             } else {
                 if ($method == \Mojavi\Request\Request::DELETE) {
                     $request->setMethod(\Zend\Http\Request::METHOD_DELETE);
                     $response = $http_client->send($request);
                 } else {
                     if ($method == \Mojavi\Request\Request::PUT) {
                         if (count($_FILES) > 0) {
                             $request->getFiles()->fromArray($_FILES);
                             $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA);
                         } else {
                             $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA);
                         }
                         if (is_array($params)) {
                             $request->getQuery()->fromArray($params);
                         }
                         $request->setMethod(\Zend\Http\Request::METHOD_PUT);
                         $response = $http_client->send($request);
                     }
                 }
             }
         }
         return $response;
     } catch (\Exception $e) {
         if (strpos($e->getMessage(), 'connect() timed out!') !== false && $retry_count < 3) {
             return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count);
         } else {
             if (strpos($e->getMessage(), 'couldn\'t connect to host') !== false && $retry_count < 3) {
                 return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count);
             } else {
                 if (strpos($e->getMessage(), 'Operation timed out') !== false && $retry_count < 3) {
                     return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count);
                 }
             }
         }
         throw $e;
     }
 }