/** * 构造POST和GET组合的请求 返回相应请求 * * @param string $url * @param array $get * @param array $post * @param boolean $returnObj * @return Zend_Http_Response false */ function doRequest($url, $get = array(), $post = array(), $returnObj = false) { try { $url = trim($url); if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new Exception('Invalid URL'); return false; } $client = new Zend\Http\Client(); $client->setUri($url); if (count($get) > 0 && is_array($get)) { $client->setParameterGet($get); } if (count($post) > 0 && is_array($post)) { $client->setParameterPost($post); } $client->setEncType(Zend\Http\Client::ENC_URLENCODED); $client->setOptions(array('maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Zend\\Http\\Client', 'timeout' => 10, 'adapter' => 'Zend\\Http\\Client\\Adapter\\Socket', 'httpversion' => Request::VERSION_11, 'storeresponse' => true, 'keepalive' => false, 'outputstream' => false, 'encodecookies' => true, 'argseparator' => null, 'rfc3986strict' => false)); if (!empty($post)) { $client->setMethod(Request::METHOD_POST); } else { $client->setMethod(Request::METHOD_GET); } $response = $client->send(); return $returnObj ? $response : $response->getBody(); } catch (Exception $e) { fb(exceptionMsg($e), \FirePHP::LOG); return false; } }
/** * 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; } }
/** * @return $this * * @throws Exception\AuthenticationFailedStatusException * @throws Exception\AuthenticationFailedAccessTokenException */ public function createAccessToken() { $request = new Request(); $request->setMethod(Request::METHOD_POST); $request->setUri($this->getConfigs()['tvkur']['api_url'] . '/oauth'); $request->getHeaders()->addHeaders(array('Accept' => 'application/json')); $oAuthConfigs = $this->getConfigs()['tvkur']['authentication']['oauth']; $request->getPost()->fromArray(array('grant_type' => $oAuthConfigs['grant_type'], 'client_id' => $oAuthConfigs['client_id'], 'client_secret' => $oAuthConfigs['client_secret'])); $client = new \Zend\Http\Client(null, array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false))); $client->setEncType(\Zend\Http\Client::ENC_URLENCODED); $response = $client->send($request); if ($response->getStatusCode() != 200) { throw new Exception\AuthenticationFailedStatusException('Tvkur api authentication failed. Body: ' . $response->getBody(), $response->getStatusCode()); } $body = json_decode($response->getBody(), true); if (empty($body['access_token'])) { throw new Exception\AuthenticationFailedAccessTokenException('Tvkur api authentication failed. Access token is empty. Body: ' . $response->getBody(), $response->getStatusCode()); } $this->setAccessToken($body['access_token']); $this->setTokenType($body['token_type']); $this->setScope($body['scope']); $this->setExpiresIn($body['expires_in']); return $this; }