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.
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:
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?
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:
Identify the url
you wish to communicate with and save it in a variable if you want.
$url = "www.wikipedia.org";
Initialize a curl
session or simply put create a new curl
resource:
$newCurl = curl_init();
Tell PHP which url
file you wish to return. We will return our earlier identified URL contents.
curl_setopt($curl, CURLOPT_URL, $url);
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.
<?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;?>
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.
<?phpfunction 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 cURLcurl_setopt($curl, CURLOPT_URL, "wikipedia.org/");// Set a different user agent string (Googlebot)// Follow redirects, if anycurl_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 codecurl_setopt($curl, CURLOPT_RETURNTRANSFER, false);// Wait 10 seconds to connect and set 0 to wait indefinitelycurl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);// Execute the cURL request for a maximum of 50 secondscurl_setopt($curl, CURLOPT_TIMEOUT, 50);// Do not check the SSL certificatescurl_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 occurredif (curl_errno($curl)){echo 'cURL error: ' . curl_error($curl);}else{// cURL executed successfullyprint_r(curl_getinfo($curl));// will display the page contents i.e its html.echo $html;}// close cURL resource to free up system resourcescurl_close($curl);}cURL();?>