Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve get with params performance #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .~lock.Hasil.ods#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,DESKTOP-2GSR8J5/Ekky,DESKTOP-2GSR8J5,27.06.2021 02:24,file:///C:/Users/Ekky/AppData/Roaming/LibreOffice/4;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this file @ekharisma

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and please Hasil.ods file convert to results.csv file

Binary file added Hasil.ods
Binary file not shown.
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$curl = new \Yuana\Curl();
$response = $curl->get('http://belanja-api.herokuapp.com');
for ($i=0; $i < 10; $i++) {
$response = $curl->get('http://belanja-api.herokuapp.com', ["users_id" => 2]);
}

echo $response;
echo $response;
39 changes: 25 additions & 14 deletions src/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
* ];
* ```
*/
class Curl {
class Curl
{

const VERSION = 'Curl-PHP-' . PHP_VERSION;

Expand Down Expand Up @@ -138,7 +139,8 @@ class Curl {
*/
private $files = array();

public function __construct() {
public function __construct()
{

$this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : self::VERSION;
}
Expand All @@ -149,8 +151,9 @@ public function __construct() {
* @param array $vars queryParams
* @return string response
*/
public function get($url, $vars = array()) {
if (!empty($vars)) {
public function get($url, $vars = null)
{
if (isset($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}
Expand All @@ -163,7 +166,8 @@ public function get($url, $vars = array()) {
* @param array $vars postFields
* @return string response
*/
public function post($url, $vars = array()) {
public function post($url, $vars = array())
{
return $this->request(self::POST, $url, $vars);
}

Expand All @@ -173,7 +177,8 @@ public function post($url, $vars = array()) {
* @param array $vars postFields
* @return string response
*/
public function put($url, $vars = array()) {
public function put($url, $vars = array())
{
return $this->request(self::PUT, $url, $vars);
}

Expand All @@ -183,7 +188,8 @@ public function put($url, $vars = array()) {
* @param array $vars postFields
* @return string response
*/
public function delete($url, $vars = array()) {
public function delete($url, $vars = array())
{
return $this->request(self::DELETE, $url, $vars);
}

Expand All @@ -193,7 +199,8 @@ public function delete($url, $vars = array()) {
* @param array $vars [file as key->value]
* @return string response
*/
public function upload($url, $vars = array()) {
public function upload($url, $vars = array())
{

foreach ($vars as $fieldName => $fileName) {
$this->files[] = [
Expand Down Expand Up @@ -253,7 +260,8 @@ private function generateBoundary()
* @param array $vars
* @return string response
*/
private function request($method, $url, $vars = array()) {
private function request($method, $url, $vars = array())
{
$this->error = '';
$this->request = curl_init();
if (is_array($vars))
Expand All @@ -271,7 +279,8 @@ private function request($method, $url, $vars = array()) {
* @param string $method
* @return void
*/
private function setRequestMethod($method) {
private function setRequestMethod($method)
{

switch ($method) {
case self::GET:
Expand All @@ -294,7 +303,8 @@ private function setRequestMethod($method) {
* @param string $vars
* @return void
*/
private function setRequestOptions($url, $vars) {
private function setRequestOptions($url, $vars)
{

curl_setopt($this->request, CURLOPT_URL, $url);
curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout);
Expand All @@ -313,7 +323,8 @@ private function setRequestOptions($url, $vars) {
* [setRequestHeaders]
* @return void
*/
private function setRequestHeaders() {
private function setRequestHeaders()
{
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key . ': ' . $value;
Expand All @@ -325,7 +336,8 @@ private function setRequestHeaders() {
* [generateResponse]
* @return string response
*/
private function generateResponse() {
private function generateResponse()
{

$response = curl_exec($this->request);
$header_size = curl_getinfo($this->request, CURLINFO_HEADER_SIZE);
Expand All @@ -340,5 +352,4 @@ private function generateResponse() {

return $content;
}

}