How to Use cURL in PHP: Simple Guide with Examples
To use
cURL in PHP, first initialize a session with curl_init(), set options using curl_setopt(), execute the request with curl_exec(), and finally close the session with curl_close(). This lets you send HTTP requests and handle responses easily.Syntax
Here is the basic syntax to use cURL in PHP:
- curl_init(): Starts a new cURL session.
- curl_setopt(): Sets options like URL, request method, headers.
- curl_exec(): Executes the request and returns the response.
- curl_close(): Closes the cURL session to free resources.
php
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://example.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
Example
This example shows how to fetch the content of a webpage using cURL in PHP and print the response.
php
<?php // Initialize cURL session $ch = curl_init(); // Set the URL to fetch curl_setopt($ch, CURLOPT_URL, "https://www.example.com"); // Return the response instead of printing it curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); // Check for errors if(curl_errno($ch)) { echo 'Error:' . curl_error($ch); } else { // Print the response echo $response; } // Close cURL session curl_close($ch); ?>
Output
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
Common Pitfalls
Common mistakes when using cURL in PHP include:
- Not setting
CURLOPT_RETURNTRANSFERtotrue, which causes the response to print directly instead of being stored. - Forgetting to close the cURL session with
curl_close(), which can waste resources. - Not checking for errors with
curl_errno()andcurl_error(), making debugging harder. - Not setting the correct HTTP method or headers when needed.
php
<?php // Wrong way: missing CURLOPT_RETURNTRANSFER $ch = curl_init("https://www.example.com"); curl_exec($ch); // prints output directly curl_close($ch); // Right way: $ch = curl_init("https://www.example.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
Quick Reference
Here is a quick cheat sheet for common cURL options in PHP:
| Option | Description |
|---|---|
| CURLOPT_URL | Set the URL to request |
| CURLOPT_RETURNTRANSFER | Return response as string instead of outputting |
| CURLOPT_POST | Set HTTP method to POST |
| CURLOPT_POSTFIELDS | Set POST data |
| CURLOPT_HTTPHEADER | Set custom HTTP headers |
| CURLOPT_TIMEOUT | Set maximum time for request in seconds |
| CURLOPT_FOLLOWLOCATION | Follow HTTP redirects |
Key Takeaways
Always initialize cURL with curl_init() and close with curl_close() to manage resources.
Set CURLOPT_RETURNTRANSFER to true to capture the response as a string.
Check for errors using curl_errno() and curl_error() after executing the request.
Use curl_setopt() to customize your HTTP request method, headers, and data.
cURL in PHP is a powerful tool to make HTTP requests and interact with web services.