-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
erika
committed
Feb 24, 2020
1 parent
37bf77f
commit 670a5a5
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} |