How to Use the PHP cURL() Function

09/04/2021

Contents

In this article, you will learn how to use the PHP cURL() function.

PHP cURL() Function

The cURL function accesses an external site and acquires various information.

This function allows you to retrieve HTML from external sites via HTTP requests.

Usage

The basic flow of cURL is as follows:

<?php
  $url = "https://plantpot.works/";
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $res =  curl_exec($curl);
  var_dump($res);
  curl_close($curl);
?>

Sets the url of the target website.

$url = "https://plantpot.works/";

Initializes a cURL session.

$curl = curl_init();

Specifies the URL to get.

curl_setopt($curl, CURLOPT_URL, $url);

Returns the execution result as a string.

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

Gets the result of executing $curl with the curl_exec function.

$res =  curl_exec($curl);