Published September 06, 2015 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. 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: # 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.
2. Setting the options. There are many options, for example, an option that defines the URL.
3. Execution with curl_exec().
4. Releasing the cURL 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.
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.
# 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:
# Handling the returned responseIn 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:
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.
In addition, we can also use the commands: curl_error and curl_errno to debug the response and receive informative error messages.
Let's see the full code:
# 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:
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.
To submit the form, the following options need to be set:
# 4. How to perform basic HTTP authentication with cURL?In order to authenticate with cURL, the following 3 options need to be set:
Let's see the code:
# 5. How to handle cookies with cURL?The use of cookies allows a website to identify returning visitors and authenticated users. To this end, cURL provides us with a mechanism through which we can save cookies. The two main options that allow us to handle cookies are:
The following code example writes the cookies into a cookie.txt file on the first visit, and then reads the data in later visits.
ConclusionUsing PHP's cURL extension provides us with a convenient way to communicate with other web sites, particularly with APIs that are provided by a third party. In the next tutorial, we will learn how to request for private details in the name of users that sign in to our website with their GitHub account. It will be done by using Github's API, and with the help of cURL. The tutorial will be a good starting point for learning how to make a social login with any social network. How we can print cURL request in php?“print curl request in php” Code Answer. $url = 'http://www.example.com';. $ch = curl_init($url);. curl_setopt($ch, CURLOPT_HEADER, true); // we want headers.. curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body.. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);. curl_setopt($ch, CURLOPT_TIMEOUT,10);. How check cURL is working in php?For anyone that wants to quickly check on the command line without creating a file: echo "<? php var_dump(extension_loaded('curl')); ?>" ... . or: php -i | grep curl or php -r 'var_dump(extension_loaded("curl"));' ^^ – hakre. ... . just put this into a phpinfo file, into the first line: ` <?. What is php cURL request?PHP cURL is a library that is the most powerful extension of PHP. It allows the user to create the HTTP requests in PHP. cURL library is used to communicate with other servers with the help of a wide range of protocols. cURL allows the user to send and receive the data through the URL syntax.
How does cURL work in php?cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.
|