Skip to content

Instantly share code, notes, and snippets.

@felipemarques
Created January 18, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipemarques/ce59e833c994ae0e45f88af812478cf4 to your computer and use it in GitHub Desktop.
Save felipemarques/ce59e833c994ae0e45f88af812478cf4 to your computer and use it in GitHub Desktop.
<?php
class ApiRest
{
protected $apiAdminEndpoint;
/**
* @param $url
* @param array $headers
* @return mixed
* @throws \Exception
*/
public function httpDelete($url, array $headers = array())
{
$url = $this->apiAdminEndpoint . ltrim($url, '/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if (!empty($err)) {
throw new \Exception($err);
}
return $response;
}
/**
* @param $url
* @param array $params
* @param array $headers
* @return mixed
* @throws \Exception
*/
public function httpPut($url, array $params = array(), array $headers = array())
{
if (isset($params['json'])) {
$params = json_encode($params['json']);
$headers[] = 'Content-Length: ' . strlen($params);
$postData = $params;
} else {
$postData = http_build_query($params);
}
$url = $this->apiAdminEndpoint . ltrim($url, '/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if (!empty($err)) {
throw new \Exception($err);
}
return $response;
}
/**
* @param $url
* @param array $params
* @param array $headers
* @return mixed
* @throws \Exception
*/
public function httpPost($url, array $params = array(), array $headers = array())
{
if (in_array('Content-type: application/json', $headers)) {
$postData = json_encode($params);
} else {
$postData = http_build_query($params);
}
$url = $this->apiAdminEndpoint . ltrim($url, '/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if (!empty($err)) {
throw new \Exception($err);
}
return $response;
}
/**
* @param $url
* @param array $headers
* @return mixed
* @throws \Exception
*/
public function httpGet($url, array $headers = array())
{
$url = $this->apiAdminEndpoint . ltrim($url, '/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if (!empty($err)) {
throw new \Exception($err);
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment