public function patchRequest($url, $data = null) { $access_token = $this->config['code']; $request_url = $this->config['endpoint'] . $url; error_log('patchRequest ' . $request_url . ' data ' . $data); $response = \Httpful\Request::patch($request_url)->addHeader('Authorization', "Bearer {$access_token}")->sendsJson()->body($data)->send(); error_log('patchRequest response ' . $response); $response = json_decode($response); if (isset($response->error)) { throw new Exception('eenvoudcrm error : ' . json_encode($response)); } return $response; }
public static function _PATCH($url, $body = '{}') { $real_url = \Marketcloud\Marketcloud::$apiBaseUrl . $url; $response = \Httpful\Request::patch($real_url)->sendsJson()->body($body)->addHeader('Authorization', \Marketcloud\Marketcloud::getAuthorizationHeader())->send(); // We check the response code // If the code is 401, then the Token might be expired. // If it is the case, we re-authenticate the client using the stored credentials. // if ($response->code == 401) { // We have to re-authenticate $auth_response = \Marketcloud\Marketcloud::authenticate(); return self::_PATCH($url, $body); } else { return $response; } }
public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) { //do the http post process $uri = $this->getFullUri($triggerData); $method = $this->getHttpMethod(); $body = http_build_query($this->getBodyParams($triggerData)); switch (strtolower($method)) { case 'post': $request = \Httpful\Request::post($uri, $body); break; case 'put': $request = \Httpful\Request::put($uri, $body); break; case 'delete': $request = \Httpful\Request::delete($uri); break; case 'head': $request = \Httpful\Request::head($uri); break; case 'patch': $request = \Httpful\Request::patch($uri, $body); break; case 'options': $request = \Httpful\Request::options($uri, $body); break; case 'get': $request = $response = \Httpful\Request::get($uri); break; default: throw new Exception('Invalid HTTP Method'); } $request->neverSerializePayload(); $request = $this->alterHttpRequestObject($request, $triggerData); $response = $request->send(); $this->handleResponse($response, $request, $triggerData); }
public function RestClient($method, $entity, $payload = null, $mime = null) { $username = $this->Login(); $password = $this->Password(); $url = $this->BaseUrl() . $entity . "?" . $payload; switch ($method) { case 'get': $response = Request::get($url)->authenticateWith($username, $password)->send(); return $response; break; case 'post': $response = Request::post($url)->body($payload, $mime)->authenticateWith($username, $password)->send(); return $response; break; case 'delete': $response = Request::delete($url)->authenticateWith($username, $password)->send(); return $response; break; case 'patch': $response = Request::patch($url)->body($payload, $mime)->authenticateWith($username, $password)->send(); return $response; break; } }
/** * Method for performing requests to PromisePay endpoints. * * @param string $method One of the four supported requests methods (get, post, delete, patch) * @param string $entity Endpoint name * @param string $payload optional URL encoded data query * @param string $mime optional Set specific MIME type. */ public static function RestClient($method, $entity, $payload = null, $mime = null) { Helpers\Functions::runtimeChecks(); if (!is_scalar($payload) && $payload !== null) { $payload = http_build_query($payload); } $url = constant(__NAMESPACE__ . '\\API_URL') . $entity . '?' . $payload; if (self::$sendAsync) { self::$pendingRequests[] = array($method, $url); // set and return an empty array instead of null // to avoid breaking any BC self::$jsonResponse = array(); return self::$jsonResponse; } switch ($method) { case 'get': $response = \Httpful\Request::get($url)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'post': $response = \Httpful\Request::post($url)->body($payload, $mime)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'delete': $response = \Httpful\Request::delete($url)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'patch': $response = \Httpful\Request::patch($url)->body($payload, $mime)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; default: throw new Exception\ApiUnsupportedRequestMethod(sprintf('%s is not a supported request method.', $method)); } self::$debugData = $response; if (self::isRetry() && ($response->http_code === 503 || $response->http_code === 504)) { if (self::$debug) { fwrite(STDOUT, sprintf("HTTP Code %d detected while retrying is enabled; retrying" . PHP_EOL, $response->http_code)); } return forward_static_call_array(array(__NAMESPACE__ . '\\PromisePay', 'RestClient'), func_get_args()); } // check for errors if ($response->hasErrors()) { $errors = Helpers\Functions::buildErrorMessage($response); switch ($response->code) { case 401: throw new Exception\Unauthorized($errors); break; case 404: if (empty($errors)) { $errors = "{$url} wasn't found."; } throw new Exception\NotFound($errors); break; default: if (empty($errors) && isset($response->raw_headers)) { list($errors) = explode("\n", $response->raw_headers); } throw new Exception\Api($errors); } } $data = json_decode($response, true); if ($data) { self::$jsonResponse = $data; } return $response; }
/** * Запрос PATCH * @param $uri * @return mixed */ public function patch($uri) { $this->request = Request::patch($uri); return $this; }
/** * * Makes a call to the API server and returns the response * * @param string $modulePath The module path (generally provided by the API Class) * @param string $id The id for a get method * @param array $params Any params that need to be added used for creating, editing, or filtering data * @param int $method The HTTP method to be used * @return mixed The result of the call */ public function makeCall($modulePath, $id = '', $params = [], $method = self::METHOD_GET) { $uri = $this->endPoint . '/' . $modulePath . (!empty(trim($id)) ? '/' . $id : ''); //Setup Request Object switch ($method) { case self::METHOD_GET: $request = Request::get($uri)->sendsJson(); break; case self::METHOD_POST: $request = Request::post($uri)->sendsJson(); break; case self::METHOD_PATCH: $request = Request::patch($uri)->sendsJson(); break; case self::METHOD_DELETE: $request = Request::delete($uri)->sendsJson(); break; default: $request = Request::get($uri)->sendsJson(); break; } $request->expectsJson(); //Handle Data if ($method == self::METHOD_GET || $method == self::METHOD_DELETE) { $params['access_token'] = $this->token; $request->uri .= '?' . http_build_query($params); } else { $request->uri .= '?access_token=' . $this->token; $request->body(json_encode($params)); } return $request->send(); }
/** * params: * id * is_hidden * is_deleted * video_url * * @param $videoID * @param $params * @param $token * @param $secure * @return bool * @throws ResponseClientError * @throws ResponseRedirectionError * @throws ResponseServerError * @throws UnknownResponseCodeException */ public static function updateVideo($videoID, $params, $token, $secure) { $uri = static::genCompositeUrl(__FUNCTION__, [':video_id' => $videoID], [], $secure); $headers = static::getHeaders($token); $response = \Httpful\Request::patch($uri)->sendsJson()->body($params)->addHeaders($headers)->send(); static::checkCode($response->meta_data['http_code'], $response->body); return $response->body; }
/** * Interface for performing requests to PromisePay endpoints * * @param string $method required One of the four supported requests methods (get, post, delete, patch) * @param string $entity required Endpoint name * @param string $payload optional URL encoded data query * @param string $mime optional Set specific MIME type. Supported list can be seen here: http://phphttpclient.com/docs/class-Httpful.Mime.html */ public static function RestClient($method, $entity, $payload = null, $mime = null) { // Check whether critical constants are defined. if (!defined(__NAMESPACE__ . '\\API_URL')) { die("Fatal error: API_URL constant missing. Check if environment has been set."); } if (!defined(__NAMESPACE__ . '\\API_LOGIN')) { die("Fatal error: API_LOGIN constant missing."); } if (!defined(__NAMESPACE__ . '\\API_PASSWORD')) { die("Fatal error: API_PASSWORD constant missing."); } if (!is_null($payload)) { if (is_array($payload) || is_object($payload)) { $payload = http_build_query($payload); } // if the payload isn't array or object, leave it intact } $url = constant(__NAMESPACE__ . '\\API_URL') . $entity . '?' . $payload; switch ($method) { case 'get': $response = Request::get($url)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'post': $response = Request::post($url)->body($payload, $mime)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'delete': $response = Request::delete($url)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; case 'patch': $response = Request::patch($url)->body($payload, $mime)->authenticateWith(constant(__NAMESPACE__ . '\\API_LOGIN'), constant(__NAMESPACE__ . '\\API_PASSWORD'))->send(); break; default: throw new Exception\ApiUnsupportedRequestMethod("Unsupported request method {$method}."); } // check for errors if ($response->hasErrors()) { $errors = static::buildErrorMessage($response); switch ($response->code) { case 401: throw new Exception\Unauthorized($errors); break; case 404: throw new Exception\NotFound($errors); default: throw new Exception\Api($errors); break; } } return $response; }