Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
web development
url

How to use the PHP Curl() function

NDUKWE CHIDERA K.

It is common in code today for scripts to need to get some resources from another webpage. This is especially common when the use of APIs is rampant.

Several scripting languages have inbuilt or third party libraries which they use to fetch information from another webpage using their URL. In PHP, the cURL library is used to achieve this.

Overview of cURL library

The cURL library is a software program developed by Daniel Sternbeg in 1996 which offers command line and scripts the ability to transfer data using URLs.

cURL can support a variety of protocols, including:

  • Http/Https
  • SMTP/SMTPs
  • FTP
  • TELNET
  • And a host of others

This library can be used on virtually any hardware or software system that wants to fetch or send information over URLs. It comes preinstalled on most modern OS, but can also be installed if not available.

To check if it is available, you can use:

curl --man

on your terminal or Gitbash. For more on cURL installation and use, check this website.

The question now is: how do you use cURL in your PHP code?

Using cURL in a PHP script

cURL stands for Client URL and is a tool used in PHP to get data from URLs provided for client usage.

To use cURL, simply follow the steps below:

Step 1

Identify the url you wish to communicate with and save it in a variable if you want.

$url = "www.wikipedia.org";

Step 2

Initialize a curl session or simply put create a new curl resource:

$newCurl = curl_init();

Step 3

Tell PHP which url file you wish to return. We will return our earlier identified URL contents.

curl_setopt($curl, CURLOPT_URL, $url);

Step 4

Now, indicate if you want the contents of that url returned with the status code or if you only want to get the contents.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

where true or 1 will mean the page contents will be returned and false or 0 means success code, not output.

This steps listed above are just guidelines to make use of cURL like in the code below. Later in this shot, we will also see some other PHP cURL methods you may want to use.

Code

<?php
//URL from which to get webpage contents.
$url = "https://www.wikipedia.org";
 
// Initialize a CURL session.
$newCurl = curl_init();
 
//grab URL and pass it to the variable.
curl_setopt($newCurl, CURLOPT_URL, $url);

// Return Page contents.
curl_setopt($newCurl, CURLOPT_RETURNTRANSFER, true);
 
$output = curl_exec($newCurl);
 
echo $output;
?>

Explanation

The code above returns the Wikipedia webpage as indicated in the url. You can pinpoint particular contents on the page and return it using the right URL.

It is also worthy to note, as mentioned earlier, that there are other PHP cURL methods like the ones identified in the code below that you can also use to improve your experience.

Code

<?php
function cURL() {

// Create a new cURL resource
$curl = curl_init();

if (!$curl) {
    die("Couldn't initialize a cURL handle");
}

// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, "wikipedia.org/");

// Set a different user agent string (Googlebot)


// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);

// Returns the status code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);

// Wait 10 seconds to connect and set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);

// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);

// Check if any error has occurred
if (curl_errno($curl))
{
    echo 'cURL error: ' . curl_error($curl);
}
else
{
    // cURL executed successfully
    print_r(curl_getinfo($curl));
    // will display the page contents i.e its html.
    echo $html;
}

// close cURL resource to free up system resources
curl_close($curl);
}
cURL();
?>

Some uses of the PHP cURL library

  • It can be used to fetch the contents of an API.
  • It can be used in web scrapping scripts.
  • It provides you a simple environment to test your API. endpoints.

RELATED TAGS

php
web development
url
RELATED COURSES

View all Courses

Keep Exploring