<?php

$post_array = array("name" => "imnobby", "year" => 2022); 
$url = "https://www.imnobby.com/";

echo imnobby_simple_http_curl($url); // HTTP CURL GET
echo imnobby_simple_http_curl($url, $post_array); // HTTP CURL POST
 
function imnobby_simple_http_curl($url, $post_array=""){

 if (!$url) {
 die("Couldn't initialize a cURL handle");
 }
 
 $curl = curl_init();
 
 $options = array(
 CURLOPT_URL => $url,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => '',
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 0,
 CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 );

 if ( $post_array ){ 
 $options[CURLOPT_CUSTOMREQUEST] = 'POST';
 $options[CURLOPT_POSTFIELDS] = $post_array;
 } else {
 $options[CURLOPT_CUSTOMREQUEST] = 'GET';
 }

 curl_setopt_array($curl, $options);

 $response = curl_exec($curl);
 
 if (curl_errno($curl)){
 die('cURL error: ' . curl_error($curl));
 }

 curl_close($curl);
 return $response;

}
Related Keywords: Developer, How-to, Solved, Simple PHP HTTP CURL GET & POST, Sample Code