Skip to content

Commit

Permalink
Initial Version
Browse files Browse the repository at this point in the history
  • Loading branch information
erika committed Feb 24, 2020
1 parent 37bf77f commit 670a5a5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor/
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "minicli/curly",
"type": "library",
"description": "Simple Curl Client",
"license": "MIT",
"homepage": "https://github.com/erikaheidi/minicli",
"keywords": ["curl","wrapper","api"],
"autoload": {
"psr-4": {
"Minicli\\Curly": "src/"
}
},
"require": {
"ext-curl": "*"
}
}
88 changes: 88 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Minicli\Util;

class Client
{
protected $last_response;

/**
* Makes a GET query
* @param string $endpoint API endpoint
* @param array $headers optional headers
* @return mixed
*/
public function get($endpoint, array $headers = [])
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $endpoint,
CURLINFO_HEADER_OUT => true
]);

return $this->getQueryResponse($curl);
}

/**
* Makes a POST query
* @param $endpoint
* @param array $params
* @param array $headers
* @return array
*/
public function post($endpoint, array $params, $headers = [])
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_URL => $endpoint,
#CURLINFO_HEADER_OUT => true,
CURLOPT_TIMEOUT => 120,
]);

return $this->getQueryResponse($curl);
}

/**
* Makes a DELETE query
* @param $endpoint
* @param array $headers
* @return array
*/
public function delete($endpoint, $headers = [])
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_URL => $endpoint,
#CURLINFO_HEADER_OUT => true,
]);

return $this->getQueryResponse($curl);
}

/**
* Exec curl and get response
* @param $curl
* @return array
*/
protected function getQueryResponse($curl)
{
$response = curl_exec($curl);
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

return [ 'code' => $response_code, 'body' => $response ];
}
}

0 comments on commit 670a5a5

Please sign in to comment.