Pemanfaatan curl untuk ‘http request’ (POST dan GET) pada PHP

<?php

if(!isset($_GET['curltype'])){
 echo "usage: curlpostget.php?curltype=post or curlpostget.php?curltype=get";
 exit(1);
}


function curl_post($url, array $postdata = NULL, array $options = array())
{
 $defaults = array(
 CURLOPT_POST => 1,
 CURLOPT_HEADER => 0,
 CURLOPT_URL => $url,
 CURLOPT_FRESH_CONNECT => 1,
 CURLOPT_RETURNTRANSFER => 1,
 CURLOPT_FORBID_REUSE => 1,
 CURLOPT_TIMEOUT => 460,
 CURLOPT_POSTFIELDS => http_build_query($postdata)
 );

$ch = curl_init();
 curl_setopt_array($ch, ($options + $defaults));
 if( ! $result = curl_exec($ch))
 {
 trigger_error(curl_error($ch));
 }
 curl_close($ch);
 return $result;
}

function curl_get($url, array $getdata = NULL)
{
 $params = '';
 foreach($getdata as $key=>$value)
 $params .= $key.'='.urlencode($value).'&';
 
 $params = trim($params, '&');
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 70); //7 detik
 curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
 curl_setopt($ch, CURLOPT_HEADER, 0);
 
 if( ! $result = curl_exec($ch))
 {
 trigger_error(curl_error($ch));
 }
 curl_close($ch);
 return $result;
}

$url = 'http://latiful.hayat.web.id/test/postgetresponse.php';
 $data = array(
 'id'=>'application/x-www-form-urlencoded',
 'key'=>"! * ' ( ) ; : @ & = + $ , / ? % # [ ]"
 );

if($_GET['curltype']=='post'){ 
 $result = curl_post($url,$data);
 }
 
 if($_GET['curltype']=='get'){ 
 $result = curl_get($url,$data);
 }
 
 echo $result;
?>