CURL can be a very important tool when you need to grab contents from a page from another website and display it on your own. Using cURL in PHP is especially useful if you’re working with an API like Twitch or X. Commonly it is used in the Linux command line, but it can be used with PHP to be much more robust and be displayed on the fly or to grab data to be used inside of PHP. Whether you want to pull in RSS feeds or even full webpages cURL is probably going to be your best bet and using it in PHP is very easy to do as long as you have the cURL Library (libcurl) installed on your server. You will also need to ensure that the cURL extension is installed and enabled in PHP. Once you have that running, you are good to go with using cURL with PHP and even on a Linux command line. Here is an example of a simple cURL call that you can use in your PHP code:
<?php
$ch = curl_init(); //Initialize cURL
// Set URL to download
curl_setopt($ch, CURLOPT_URL, "http://www.lepslair.com");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
?>
And that is it. Right there is a basic cURL example. What does it mean though? Well first we have to initialize cURL and store it in a variable, that’s what curl_init(); does, it is stored in $ch. From here we can add options with the curl_setopt function. We first set the URL, which is https://lepslair.com. Then we include the headers and return the output to store into a variable instead of just display it. With the page code in a variable we could then store the output in a database or file if we wanted to. Lastly, we set the maximum timeout, we don’t want this code to try connecting indefinitely, so we give it a 10 second timeout. Next we execute it by using the curl_exec and giving it the $ch variable. Then we use curl_close function to close the cURL connection.
The above code is just the beginning. You could have the same code going to a separate URL using a different variable to differentiate between the two, say $dh along with $ch. Then you could make two simultaneous cURL calls to two different URLs.
The above is a very basic example, we can add more curl_setopt functions to include more options. These options include sending POST data, additional headers, etc. To use curl_setopt specifically, you need the variable that curl_init() is stored in, the constant option, and then the value for it. Below is the various constants you can use, a description of them, and an example of how to do it.
Option | Description | Example |
---|---|---|
CURLOPT_URL |
Specifies the URL to fetch or send data to. | curl_setopt($ch, CURLOPT_URL, "http://example.com"); |
CURLOPT_RETURNTRANSFER |
Returns the transfer as a string of the return value instead of outputting it directly. | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
CURLOPT_POST |
Sets the request method to POST. Used to send data to the server. | curl_setopt($ch, CURLOPT_POST, true); |
CURLOPT_POSTFIELDS |
Specifies the data to be sent in a POST request. Can be an array or a URL-encoded string. | curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John"); |
CURLOPT_HTTPHEADER |
Sets custom HTTP headers for the request. | curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]); |
CURLOPT_FOLLOWLOCATION |
Follows any “Location:” headers that the server sends as part of the HTTP header. | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
CURLOPT_TIMEOUT |
Sets the maximum time in seconds that the request is allowed to take. | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
CURLOPT_CONNECTTIMEOUT |
Sets the maximum time in seconds to wait while trying to connect. | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
CURLOPT_SSL_VERIFYPEER |
Checks the authenticity of the SSL certificate. Typically set to true for secure connections. |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
CURLOPT_SSL_VERIFYHOST |
Verifies the SSL certificate’s hostname. Usually set to 2 for strict verification. |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
CURLOPT_USERAGENT |
Sets the “User-Agent” header to emulate a specific browser. | curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0"); |
CURLOPT_COOKIE |
Sets the contents of the “Cookie:” header to be sent with the request. | curl_setopt($ch, CURLOPT_COOKIE, "user=John"); |
CURLOPT_REFERER |
Sets the “Referer:” header, which indicates the page that referred the request. | curl_setopt($ch, CURLOPT_REFERER, "http://example.com"); |
CURLOPT_CUSTOMREQUEST |
Specifies a custom request method, such as PUT or DELETE . |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
CURLOPT_HEADER |
Includes the header in the output when set to true . |
curl_setopt($ch, CURLOPT_HEADER, true); |
CURLOPT_VERBOSE |
Enables verbose output for debugging purposes. | curl_setopt($ch, CURLOPT_VERBOSE, true); |
CURLOPT_USERPWD |
Sends a username and password for HTTP authentication in the format username:password . |
curl_setopt($ch, CURLOPT_USERPWD, "user:password"); |
CURLOPT_PROXY |
Specifies a proxy to use for the request. | curl_setopt($ch, CURLOPT_PROXY, "http://proxy.com:8080"); |
CURLOPT_HTTPPROXYTUNNEL |
Tunnels through a given HTTP proxy. | curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); |
CURLOPT_COOKIEFILE |
Specifies the file to read cookies from. | curl_setopt($ch, CURLOPT_COOKIEFILE, "/path/to/cookiefile.txt"); |
CURLOPT_COOKIEJAR |
Specifies the file to write cookies to after the request is completed. | curl_setopt($ch, CURLOPT_COOKIEJAR, "/path/to/cookiefile.txt"); |
CURLOPT_MAXREDIRS |
Limits the number of redirects to follow if CURLOPT_FOLLOWLOCATION is set. |
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
CURLOPT_INFILE |
Specifies a file to upload using the PUT method. |
curl_setopt($ch, CURLOPT_INFILE, $fp); |
CURLOPT_UPLOAD |
Allows file uploading via PUT . |
curl_setopt($ch, CURLOPT_UPLOAD, true); |
CURLOPT_FILE |
Specifies the file to save the output to instead of displaying it directly. | curl_setopt($ch, CURLOPT_FILE, $fp); |
With these options you will be able to upload files, send headers, send usernames and passwords, the possibilities are nearly limitless. If you need more help with it, check out the official PHP.net documentation.