PHP-Send HTTP Get/Post Request and Read JSON response
PHP provides an easy way to send HTTP GET and POST requests and read the JSON response. In this article, we will show you how to do it.
To send a GET request, use the following code:
$url = ”
$request = file_get_contents($url);
To send a POST request, use the following code:
$url = ”
$request = “name=John&age=30”;
$postData = “”;
$headers = array(“Content-type: application/x-www-form-urlencoded”);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
To read the JSON response, use the following code:
$json = file_get_contents(”
json_decode($json);