How To Make HTTP Requests in PHP - curl() Function

cURL means client URL. It allows you to connect with other URLs and use their responses in your code.

Use curl in PHP is very simple process:

  1. Initialize a curl session
  2. Set various options for the session
  3. Execute and fetch data from server or send data to server
  4. Close the session
// Initialize curl session
$ch = curl_init();

// Set options
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

// Execute
$result = curl_exec($ch);

// Close curl session

// Output result
echo $result;

Setting URL option with the $url variable passed into the function:

curl_setopt($ch, CURLOPT_URL, $url);

Setting option to return the webpage data:



Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive