/** * Send request elasticsearch like this : * curl -X{$httpMethod} http://host/type/index/{$elasticMethod} -d '{json_decode($content)}' * @param int $httpMethod * @param string $elasticMethod * @param string $content * * @return Stdlib\ResponseInterface */ public function sendRequest($httpMethod = Request::METHOD_GET, $elasticMethod = null, $content = null) { $request = new Request(); $request->setUri($this->url . $elasticMethod)->setMethod($httpMethod)->setContent($content); $client = new Client(); return $client->dispatch($request); }
function PlanJSONManager($action, $url, $requestjson, $uid) { $request = new Request(); $request->getHeaders()->addHeaders(array('Content-Type' => 'application/json; charset=UTF-8')); //$url=""; try { $request->setUri($url); $request->setMethod($action); $client = new Client(); if ($action == 'PUT' || $action == 'POST') { $client->setUri($url); $client->setMethod($action); $client->setRawBody($requestjson); $client->setEncType('application/json'); $response = $client->send(); return $response; } else { $response = $client->dispatch($request); //var_dump(json_decode($response->getBody(),true)); return $response; } } catch (\Exception $e) { $e->getTrace(); } return null; }
protected function _replace($filePath, $photoId, $async = 0) { $params['async'] = $async; $params['photo_id'] = $photoId; $finalParams = $this->_httpUtility->assembleParams($this->_endpointReplace, $this->_configOAuth, $params); $request = new \Zend\Http\Request(); $request->setUri($this->_endpointReplace)->setMethod('POST')->setPost(new Parameters($finalParams)); $this->_httpClient->reset(); $this->_httpClient->setRequest($request); $this->_httpClient->setEncType(\Zend\Http\Client::ENC_FORMDATA, 'ITSCARO'); $this->_httpClient->setFileUpload($filePath, 'photo'); $response = $this->_httpClient->dispatch($request); $decodedResponse = simplexml_load_string($response->getBody()); if (!$decodedResponse instanceof \SimpleXMLElement) { throw new \Exception('Could not decode response: ' . $response->getBody(), self::ERR_RESPONSE_NOT_XML); } else { if ($decodedResponse['stat'] == 'ok') { if ($async) { return (string) $decodedResponse->ticketid; } else { return (string) $decodedResponse->photoid; } } else { throw new \Exception((string) $decodedResponse->err['msg'], (int) $decodedResponse->err['code']); } } }
public function connect($uri, $post = false, $params = null) { $client = new Client($uri, array('timeout' => 600, 'sslverifypeer' => false)); if (!$post) { $client->setParameterGet($params); } $request = $client->getRequest(); $response = $client->dispatch($request); return $response; }
public function testPost() { $request = new HttpRequest(); $request->setUri(Form::OGONE_TEST_URL); $request->setMethod(HttpRequest::METHOD_POST); $request->getPost()->set('PSPID', $this->form->getParam('PSPID')); $request->getPost()->set('orderID', $this->form->getParam('orderID')); $request->getPost()->set('amount', $this->form->getParam('amount')); $request->getPost()->set('currency', $this->form->getParam('currency')); $request->getPost()->set('language', $this->form->getParam('language')); $request->getPost()->set('CN', $this->form->getParam('CN')); $request->getPost()->set('EMAIL', $this->form->getParam('EMAIL')); $request->getPost()->set('accepturl', $this->form->getParam('accepturl')); $request->getPost()->set('declineurl', $this->form->getParam('declineurl')); $request->getPost()->set('exceptionurl', $this->form->getParam('exceptionurl')); $request->getPost()->set('cancelurl', $this->form->getParam('cancelurl')); $request->getPost()->set('SHASign', $this->form->getSha1Sign()); $request->getHeaders()->addHeader(\Zend\Http\Header\ContentType::fromString('Content-type: application/x-www-form-urlencoded')); $response = $this->httpClient->dispatch($request); $this->assertEquals(200, $response->getStatusCode(), 'Ogone response does not have the correct HTTP status code'); $this->assertSelectCount('form[name="OGONE_CC_FORM"]', 1, $response, 'Ogone response does not include the correct form'); }
public function send($url) { $request = new Request(); $request->getHeaders()->addHeaders(array('Content-Type: application/xml; charset=ISO-8859-1')); $request->setUri($url); $request->setMethod(Request::METHOD_GET); // $request->setContent($checkout->parseXML()); $client = new Client(); $client->setOptions(array('sslverifypeer' => false)); $response = $client->dispatch($request); if ($response->getBody() == 'Unauthorized') { throw new \Exception('Unauthorized access to PagSeguro'); } return simplexml_load_string($response->getBody()); }
public function call($params = null) { $request = new Request(); $request->getHeaders()->addHeaders(array('Accept' => 'application/json')); if (!is_null($this->bearer_token)) { $request->getHeaders()->addHeaders(array('Authorization' => $this->bearer_token)); } $request->setUri($this->endpoint); $request->setMethod($this->method); if (!is_null($params)) { $request->getPost()->fromArray($params); } $client = new Client($this->endpoint, array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl')); $response = $client->dispatch($request); return Json::decode($response->getBody(), Json::TYPE_ARRAY); }
protected function get($url, $data, $options = array()) { $request = new Request(); $request->setUri($url); $request->setMethod('GET'); $request->getQuery()->fromArray($data); $client = new Client(); $client->setOptions($options); $response = $client->dispatch($request); try { $result = Json::decode($response->getBody(), Json::TYPE_ARRAY); return $result; } catch (RuntimeException $e) { return $response->getBody(); } }
/** * @see Athem\Service\Service#call() * @return $this */ public function call() { $request = new Request(); $request->setUri($this->options['url']); $response = new Response(); $client = new Client($this->options['url'], array('useragent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36')); $client->setAdapter(new \Zend\Http\Client\Adapter\Curl()); $client->setParameterGet(array('lang' => $this->options['lang'])); $response = $client->dispatch($request, $response); $this->data = explode("\n", $response->getContent()); foreach ($this->data as $i => $row) { if (!empty($row) && !empty($row[0])) { $this->data[$i] = explode("\t", $row); } else { unset($this->data[$i]); } } array_shift($this->data); return $this; }
/** * @param Request $request * @return array */ public function dispatchRequest(Request $request) { if ($this->profiler) { $this->getProfiler()->profilerStart(); } // Send request /** @var $response Response */ $response = $this->httpClient->dispatch($request); if ($this->profiler) { $this->getProfiler()->profilerFinish($this->httpClient); } $this->lastRequest = $request; $this->lastResponse = $response; $validStatusCodes = $this->getValidStatusCodes(); $responseStatusCode = $response->getStatusCode(); $decodedResponse = (array) $this->getResponseDecoder()->decode($response); if (empty($validStatusCodes) && $response->isSuccess() || in_array($responseStatusCode, $validStatusCodes)) { return $decodedResponse; } throw $this->getInvalidResponseException($decodedResponse, $response); }
private function send(Checkout $checkout) { $request = new Request(); $request->getHeaders()->addHeaders(array('Content-Type: application/xml; charset=ISO-8859-1')); $request->setUri($this->options->getWsUrl()); $request->setMethod(Request::METHOD_POST); $request->setContent($checkout->parseXML()); $client = new Client(); $response = $client->dispatch($request); if ($response->getBody() == 'Unauthorized') { throw new \Exception('Unauthorized access to PagSeguro'); } $xml = ''; try { $xml = simplexml_load_string($response->getBody()); } catch (\Exception $e) { throw new \Exception('Error on parse reponse xml. ' . $response->getBody(), 500, $e); } if ($xml->code) { return $xml->code; } throw new \Exception('An error has occurred: ' . $response->getBody()); }
/** * Execute/Dispatch the request. * @param Client $client * @param Request $request * @throws \Exception * @return \FrontCore\Models\ApiRequestModel */ private function executeRequest(Client $client, $request = NULL) { //check if api location isset if ($this->api_url == "") { throw new \Exception(__CLASS__ . " : Line " . __LINE__ . " : Request could not be performed, API Location is not set", 500); } //end if //should session login information be disabled? if ($this->api_session_login === TRUE) { //load user session data $objUserSession = FrontUserSession::isLoggedIn(); //check if this is a user or site call if ($this->api_pword == "" || !$this->api_pword) { //try to extract from session if (is_object($objUserSession)) { $this->setAPIUserPword($objUserSession->pword); } //end if } //end if //set api username if ($this->api_user == "" || !$this->api_user) { //is api key encoded? if (is_object($objUserSession)) { if (isset($objUserSession->api_key_encoded) && $objUserSession->api_key_encoded === TRUE) { $key = $this->getServiceLocator()->get("FrontCore\\Models\\FrontCoreSecurityModel")->decodeValue($objUserSession->uname); $this->setAPIUser($key); } else { //try to extract from session $this->setAPIUser($objUserSession->uname); } //end if } //end if } //end if //set api key if ($this->api_key == "" || !$this->api_key) { //is api key encoded? if (is_object($objUserSession)) { if (isset($objUserSession->api_key_encoded) && $objUserSession->api_key_encoded === TRUE) { $key = $this->getServiceLocator()->get("FrontCore\\Models\\FrontCoreSecurityModel")->decodeValue($objUserSession->api_key); $this->setAPIKey($key); } else { //try to extract from session $this->setAPIKey($objUserSession->api_key); } //end if } //end if } //end if require "./config/helpers/ob1.php"; //@TODO - create own api authentication logic // throw new \Exception(__CLASS__ . " : Line " . __LINE__ . ": Implement your api request header logic here", 9999); } else { if ($this->api_key != "") { require "./config/helpers/ob2.php"; //@TODO - create own api authentication logic //throw new \Exception(__CLASS__ . " : Line " . __LINE__ . ": Implement your api request header logic here", 9999); } else { //bypass to perform info request $arr_headers = array(); } //end if } //end if //use manually set headers and then clear them if (is_array($this->arr_manual_request_headers)) { $arr_headers = $this->arr_manual_request_headers; $this->arr_manual_request_headers = FALSE; } //end if try { //set user logged in flag for submit to api if ($objUserSession) { $arr_headers["m3userloggedin"] = time(); } //end if //set origin url $arr_headers['m3originurl'] = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //load event manager $event = new EventManager(); //trigger pre event $result = $event->trigger("apiCallExecuted.pre", $this, array("objClient" => $client, "objRequest" => $request, 'url' => self::buildURI())); //set timeout $client->setOptions(array("timeout" => 60, "sslverifypeer" => FALSE)); if ($request instanceof Request) { $client->setHeaders($arr_headers); $response = $client->dispatch($request); } else { $client->setUri(self::buildURI()); $client->setHeaders($arr_headers); $response = $client->send(); } //end if $arr_api_data = array("url" => self::buildURI(), "response" => $response->getBody()); //trigger post event $result = $event->trigger("apiCallExecuted.pre", $this, array("objApiData" => (object) $arr_api_data, "objResponse" => $response, "objClient" => $client, "objRequest" => $request, 'url' => self::buildURI())); $event->trigger("apiCallExecuted", $this, array("objApiData" => (object) $arr_api_data, "objResponse" => $response)); //resest the module indicator where set to null if (is_null($this->api_module)) { $this->api_module = "api"; } //end if return self::processResponse($response); } catch (\Exception $e) { throw new \Exception(__CLASS__ . " : Line " . __LINE__ . " : An error occured performing api request. URL : " . self::buildURI() . " : Error ||" . $e->getMessage(), $e->getCode()); } //end function }
function getBOTHSATResult($gpa, $satcrm, $satwrt, $county, $state, $freelunch, $us, $major, $csize, $cpref, $allflag, $schoolname) { $request = new Request(); $request->getHeaders()->addHeaders(array('Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8')); if ($county == null) { $county = ""; } if ($major == "-1") { $major = null; } $url = ""; try { $request->setUri(Constants::BOTH_URI_SAT); $request->setMethod('GET'); if ($allflag == "1") { $request->setQuery(new Parameters(array('gpa' => $gpa, 'satcrm' => $satcrm, 'satwrt' => $satwrt, 'county' => $county, 'state' => $state, 'size' => $csize, 'style' => $cpref, 'lunch' => $freelunch, 'majors' => $major, "allcolleges" => 'true', 'school' => $schoolname))); } else { $request->setQuery(new Parameters(array('gpa' => $gpa, 'satcrm' => $satcrm, 'satwrt' => $satwrt, 'county' => $county, 'state' => $state, 'size' => $csize, 'style' => $cpref, 'lunch' => $freelunch, 'majors' => $major, 'school' => $schoolname))); } $url = Constants::BOTH_URI_SAT . '?' . $request->getQuery()->toString(); $client = new Client(); $response = $client->dispatch($request); $data = json_decode($response->getBody(), true); } catch (\Exception $e) { $data['exception'] = "URL not found"; $data['url'] = $url; } return $data; }
public function httpClientAction() { # Shows making a HTTP request and seeing response $request = new Request(); $request->setUri('http://example.org'); $client = new Client(); #$client->setRequest($request); $response = $client->dispatch($request); return array('response' => $response); }
/** * @param array $data * * @return string * @throws Exception\RuntimeException */ private function getFile(array $data) { $data = array_merge(array('api_token' => $this->options->getApiToken(), 'id' => $this->options->getProjectId(), 'action' => 'export'), $data); $fileKey = md5(json_encode($data)); if (isset($this->files[$fileKey])) { return $this->files[$fileKey]; } $client = new Http\Client(null, $this->options->getClient()); $request = new Http\Request(); $request->setUri($this->options->getUrl()); $request->setMethod(Http\Request::METHOD_POST); $request->setContent(http_build_query($data)); usleep($this->options->getRequestDelay()); /** @var \Zend\Http\Response $response */ $response = $client->dispatch($request); if (!$response->isSuccess()) { throw new Exception\RuntimeException('Request was not successful'); } /** @var \stdClass $content */ $content = json_decode($response->getContent()); if ($content->response->code != 200) { throw new Exception\RuntimeException($content->response->message); } $this->files[$fileKey] = $content->item; return $this->files[$fileKey]; }
private function sendCurlRequest($url, $postString, $method = Request::METHOD_POST) { $request = new Request(); $request->setUri($url); $request->setMethod($method); $request->setContent($postString); $client = new Client(); $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); $response = $client->dispatch($request); return $response->getContent(); }