Working on the server side does not necessarily imply that all the required information needs to be present on the database of the site on which we are working. As a matter of fact, growing parts of the information that is used on the server side comes from external sources, often via an API that allows some kind of service to provide information to the website without having access to the database on the remote site. To utilize this information, we can use the cURL built-in PHP extension.

Đang xem: How to use curl with php

cURL is a PHP extension, that allows us to receive and send information via the URL syntax. By doing so, cURL makes it easy to communicate between different websites and domains. This tutorial includes 5 common cases for the use of cURL, and they include:

*

Joseph Benharosh is a full stack web developer and the author of the eBook The essentials of object oriented PHP.

# How does the cURL extension work?

cURL works by sending a request to a web site, and this process includes the following four parts:

1. Initialization.

$handle = curl_init();2. Setting the options. There are many options, for example, an option that defines the URL.

curl_setopt($handle, CURLOPT_URL, $url);3. Execution with curl_exec().

$data = curl_exec($handle);4. Releasing the cURL handle.

curl_close($handle);The second part is the most interesting because it allows us to define how cURL works in a highly accurate manner, by using the many options it has to offer.

# 1. How to download the contents of a remote website to a local file?

In order to download the contents of a remote web site, we need to define the following options:

CURLOPT_URL- Defines the remote URL.

CURLOPT_RETURNTRANSFER- Enables the assignment of the data that we download from the remote site to a variable. In this example, we assign the data into the variable $output.

Xem thêm: Trải Nghiệm Nội Thất Rạp Chiếu Phim Imax Là Gì Cgv Việt Nam, Xem Phim Bằng Công Nghệ Imax Ở Đâu

$handle = curl_init();$url = “https://www.ladygaga.com”;// Set the urlcurl_setopt($handle, CURLOPT_URL, $url);// Set the result output to be a string.curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);$output = curl_exec($handle);curl_close($handle);echo $output;When we print the value of the variable $output to the screen, we will see the local version of the web site for the world”s best singer.

*

The options can be written more compactly using curl_setopt_array(), which is a cURL function that convenes the options into an array.

curl_setopt_array($handle,array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true));

# 2. How to download a file from a remote site using cURL?

A remote file can be downloaded to our server, if the option CURLOPT_ FILE is set. For example, the following code downloads the book “The Divine Comedy” from Project Gutenberg into a the_divine_comedy.html file on our server:

// The distant site url.$url = “https://www.gutenberg.org/files/46852/46852-h/46852-h.htm”;// The file on our server.$file = __DIR__ . DIRECTORY_SEPARATOR . “the_divine_comedy.html”;$handle = curl_init();// Open the file on our server for writing.$fileHandle = fopen($file, “w”);curl_setopt_array($handle,array( CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle,));$data = curl_exec($handle);curl_close($handle);fclose($fileHandle);

# Handling the returned response

In order to get the parameters of the response for it to be monitored and debugged, we need to set the option CURLOPT_HEADER. For example:

$url = “”https://www.gutenberg.org/files/41537/41537-h/41537-h.htm”;$file = __DIR__ . DIRECTORY_SEPARATOR . “the_divine_comedy.html”;$handle = curl_init();$fileHandle = fopen($file, “w”);curl_setopt_array($handle,array(CURLOPT_URL => $url,CURLOPT_FILE => $fileHandle,CURLOPT_HEADER => true));$data = curl_exec($handle);To get additional information about the request, we use the curl_getinfo command that enables us to receive important technical information about the response, including the status code (200 for success) and the size of the downloaded file.

$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);$downloadLength = curl_getinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);In addition, we can also use the commands: curl_error and curl_errno to debug the response and receive informative error messages.

if(curl_errno($handle)){print curl_error($handle);}Let”s see the full code:

$url = “https://www.gutenberg.org/files/46852/46852-h/46852-h.htm”; $file = __DIR__ . DIRECTORY_SEPARATOR . “the_divine_comedy.html”; $handle = curl_init(); $fileHandle = fopen($file, “w”); curl_setopt_array($handle, array( CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle, CURLOPT_HEADER => true )); $data = curl_exec($handle); $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); $downloadLength = curl_getinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD); if(curl_errno($handle)){ print curl_error($handle);}else{ if($responseCode == “200”) echo “successful request”; echo ” # download length : ” . $downloadLength; curl_close($handle); fclose($fileHandle);}

# 3. How to submit forms with cURL?

Until this moment, we have demonstrated the use of the GET method of HTTP (which is generally used to watch and download content). cURL can also make use of the POST method of HTTP in order to submit forms.

In order to demonstrate form submission with cURL, we need to create the following two files:

index.php in which we put the cURL script. form.php in which we put the form to be submitted.

Xem thêm: How To Install And Use Php Composer On Centos 7 Hostnextra, How To Install Composer On Centos 7

The form.php will be in reality, found on a remote server (although, for the sake of the example, both files may be placed on the same server). Also, for the example, we will use a form with 3 fields: firstName, lastName and submit.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *