/** * Creates a new HTTP POST request with the appropriate content, headers * and such, which can then be used to send JSON-RPC requests. * @param string $content the request content * @return \Zend\Http\Request the request * @throws ClientException if the Content-Type header cannot be set */ private function createHttpRequest($content) { $httpRequest = new \Zend\Http\Request(); $httpRequest->setUri($this->_endPoint); $httpRequest->setMethod(\Zend\Http\Request::METHOD_POST); $httpRequest->setContent($content); // Set headers $headers = $httpRequest->getHeaders(); if (!$headers instanceof \Zend\Http\Headers) { throw new ClientException('Unable to configure HTTP headers'); } $headers->addHeaderLine('Content-Type', 'application/json'); $httpRequest->setHeaders($headers); return $httpRequest; }
public static function sendRequest($url, $postString = '', $method = \Zend\Http\Request::METHOD_POST) { $client = new \Zend\Http\Client(); $client->setAdapter(new \Zend\Http\Client\Adapter\Curl()); $request = new \Zend\Http\Request(); $request->setUri($url); $request->setMethod($method); if (is_array($postString)) { $params = $postString; $postString = ''; foreach ($params as $key => $value) { $postString .= $key . '=' . urlencode($value) . '&'; } $postString = substr($postString, 0, strlen($postString) - 1); } $request->setContent($postString); $response = $client->dispatch($request); return $response->getContent(); }
/** * Creates a basic HTTP request * @param string $relativeUrl the relative API URL * @return \Zend\Http\Request */ protected function createBaseRequest($relativeUrl) { $baseUrl = $this->getBaseUrl(false); $request = new \Zend\Http\Request(); $request->setUri($baseUrl . $relativeUrl); $request->setMethod(\Zend\Http\Request::METHOD_POST); $request->getHeaders()->addHeaders(array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept-Encoding' => 'identity')); // plain text $this->addDefaultParameters($request); return $request; }
/** * 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; } }
public function getToken($data = null) { $config = $this->getConfig(); $request = new \Zend\Http\Request(); $request->setUri($config['zf-oauth2']['oauthServerUri']); $request->setMethod('POST'); $client = new \Zend\Http\Client(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $client->setAdapter($adapter); $adapter->setOptions(array('curloptions' => array(CURLOPT_POST => 1, CURLOPT_POSTFIELDS => 'grant_type=client_credentials', CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $config['zf-oauth2']['client'] . ':' . $config['zf-oauth2']['client_pwd'], CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_SSL_VERIFYHOST => FALSE))); $response = $client->dispatch($request); // I need to extract only the JSon content preg_match('~\\{(?:[^{}]|(?R))*\\}~', $response->getContent(), $jsonArray); // From the JSonArray I only need the first element - 0 $response = json_decode($jsonArray[0], true); $token = $response['access_token']; // Instantiate the AccessTokens Controller in order to be able to update the record $OauthAccessTokensController = $this->getServiceLocator()->get('OauthAccessTokensController'); $OauthAccessTokensController->setValidateOAuth(false); $responseOAT = $OauthAccessTokensController->update($token, array(USERID => $data[USERID] . '')); if ($responseOAT->getVariable('status') == STATUS_FAILED) { // TODO - Log to admin, Something bad happened, this should work without any problem throw new \Exception($responseOAT->getVariable('msg')); } else { $response[USERID] = $data[USERID]; } /* if ( $OauthAccessTokensController->getResponse( )->getStatusCode( ) !== 200 ) { // TODO - Log to admin, Something bad happened, this should work without any problem throw new CommonException ( array( 'messageId' => 'globals.query.record_not_found' ,'parms' => array( 'id' => $token ) ,EXCEPTION_ERRORKEY => 404 ) ); } else { $response[ USERID ] = $data[ USERID ]; } */ return $response; }
if (!$location) { printf("No location header\n"); exit; } $data = $location->uri()->getQueryAsArray(); if (!isset($data['code'])) { die("No code in response\n"); } /* * Token request */ $client->resetParameters(); $client->setMethod('POST'); $request = new \Zend\Http\Request(); $request->setUri($tokenUri); $request->setMethod('POST'); $request->getPost()->fromArray(array('grant_type' => 'authorization_code', 'code' => $data['code'], 'redirect_uri' => 'https://dummy', 'client_id' => $clientId)); // Client authentication $request->getHeaders()->addHeaders(array('Authorization' => $clientAuthorization)); _dumpRequest($request); $client->setMethod('POST'); $response = $client->send($request); _dumpResponse($response); $tokenData = \Zend\Json\Json::decode($response->getContent(), \Zend\Json\Json::TYPE_ARRAY); _dump($tokenData); if (isset($tokenData['error'])) { die("ERROR\n"); } /* * User info request */