First need to install curl module than can use below code to make GET and POST request using PHP.
public function makeCURLRequest($url, $data = null, $method = 'GET') { if (empty($url) OR empty($data)) { return 'Error: invalid Url or Data'; } //url-ify the data for the get : Actually create datastring $fields_string = ''; foreach ($data as $key => $value) { $fields_string .= $key . '=' . urlencode($value) . '&'; } $fields_string = rtrim($fields_string, '&'); $ch = curl_init(); if ($method == 'GET') { $urlStringData = $url . '?' . $fields_string; // Set query data here with the URL curl_setopt($ch, CURLOPT_URL, $urlStringData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_TIMEOUT, '10'); } elseif ($method == 'POST') { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); } if (curl_exec($ch) === false) { $error = curl_error($ch); curl_close($ch); throw new Exception('Curl error in url : ' . $url . " " . $error); } else { $content = trim(curl_exec($ch)); } curl_close($ch); return $content; } }
The post Make Get and Post request using curl in php appeared first on Techathon.