Skip to content
Snippets Groups Projects
Commit 247a4833 authored by Fawwaz Anugrah Wiradhika Dharmasatya's avatar Fawwaz Anugrah Wiradhika Dharmasatya
Browse files

feat: menambahkan library untuk fetch api

parent e55dc228
No related merge requests found
<?php
namespace Lib\Service\Api;
use PDO;
class FetchAPI{
private string $host;
public function __construct(string $host)
{
$this->host = $host;
}
public function get(string $endpoint,array $param, array $headers){
$url = $this->host . $endpoint;
//add param
if(count($param)>0){
$url .= "?";
}
foreach($param as $key=>$value){
$url = $url . $key . "=" . $value . "&";
}
$ch = curl_init($url);
//set curl header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
//set http header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function post(string $endpoint,array $param, array $headers){
$url = $this->host . $endpoint;
//add param
$ch = curl_init($url);
//set curl header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
//set post payload
curl_setopt($ch, CURLOPT_POST, 1);
$post_payload = "";
foreach($param as $key=>$value){
$post_payload = $post_payload . $key . "=" . $value . "&";
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_payload);
//set http header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment