DEV Community

Step-by-Step Guide on How to Use cURL in PHP

Ever needed to pull data quickly from the web in a PHP app? If so, cURL is your best friend. It’s an essential tool for developers, data scientists, or anyone who works with APIs, authentication, or web scraping. Today, I’m diving into how you can leverage cURL in PHP to seamlessly grab data from the web using proxies and their API.
Let’s cut through the jargon and get hands-on. We’ll start with setting up your environment and walk through practical examples with real-time applications. Whether you're scraping websites or making API requests, you’ll walk away with actionable knowledge you can use today.

Step 1: Ready Your PHP Environment

First, you’ll need PHP up and running. For MacOS or Linux, you can quickly install it via the terminal:
brew install php
On Windows? Just head over to PHP's official site and grab the latest version.
Once installed, create a file called index.php in your preferred directory and spin up a local server:
php -S localhost:8000
Now, let’s verify if cURL is available. Add this to your index.php:

<?php  
  phpinfo();  
?>  
Enter fullscreen mode Exit fullscreen mode

Visit localhost:8000 in your browser and search for "curl." If you don’t see it, open your terminal and find your php.ini file:
php --ini
Find the line extension=curl in your php.ini file, uncomment it, restart your server, and you’re good to go.

Step 2: Basic cURL Syntax and Request Structure

Now that your environment’s set, let’s dive into the code. cURL in PHP follows a simple pattern:
Initialize the session.
Set session options.
Execute and retrieve the response.
Here’s a quick example. We’ll make a basic GET request to fetch our public IP:

<?php  
$url = "https://example.com/";

// Initialize session  
$session = curl_init();

curl_setopt($session, CURLOPT_URL, $url); // Set URL  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Return as string  
$response = curl_exec($session); // Execute  

if (curl_errno($session)) { // Check for errors  
    echo 'cURL Error: ' . curl_error($session);  
} else {  
    echo "Response: \n";  
    echo $response;  
}

curl_close($session); // Close session  
?>  
Enter fullscreen mode Exit fullscreen mode

Visit localhost:8000, and you’ll see your current IP address as the output.

Step 3: Adding Authentication to Your cURL Requests

For some requests, you’ll need to authenticate. No worries—just use CURLOPT_USERPWD to send your credentials, paired with CURLOPT_HTTPAUTH for basic auth.
Here’s an example of how you’d add simple authentication to our request:

<?php  
$url = "https://example.com/";

// Initialize session  
$session = curl_init();

curl_setopt($session, CURLOPT_URL, $url);  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($session, CURLOPT_USERPWD, "username:password"); // Set credentials  
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // Basic Auth  
$response = curl_exec($session); // Execute  

if (curl_errno($session)) {  
    echo 'cURL Error: ' . curl_error($session);  
} else {  
    echo "Response: \n";  
    echo $response;  
}

curl_close($session); // Close session  
?>  
Enter fullscreen mode Exit fullscreen mode

Easy, right? Your credentials are now embedded, and you're good to go.

Step 4: Handling Proxies with cURL

If you're dealing with geo-blocked content or need to mask your requests, using proxies is a must. Here’s how you can route your cURL requests through a proxy:

  1. Set the proxy address.
  2. Add proxy authentication credentials. Here’s a quick example using proxies:
<?php  
$url = "https://example.com/";  
$proxy = 'proxy.example.com:7777';  
$username = 'USERNAME';  
$password = 'PASSWORD';

// Initialize session  
$session = curl_init();

curl_setopt($session, CURLOPT_URL, $url); // Set URL  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($session, CURLOPT_PROXY, "http://$proxy"); // Set proxy  
curl_setopt($session, CURLOPT_PROXYUSERPWD, "customer-$username:$password"); // Proxy auth  
$response = curl_exec($session); // Execute  

if (curl_errno($session)) {  
    echo 'cURL Error: ' . curl_error($session);  
} else {  
    echo "Response: \n";  
    echo $response;  
}

curl_close($session); // Close session  
?>  
Enter fullscreen mode Exit fullscreen mode

With this setup, your requests go through a proxy server—great for scraping and handling sensitive data without revealing your real IP.

Step 5: Fetching Data via API

If you want to scrape data from the web, the API is your ticket. Let’s set it up.
We’ll send a request to the sandbox and retrieve some data:

<?php  
$url = "https://api.example.com/v1/queries";  
$username = "USERNAME";  
$password = "PASSWORD";  

$params = [  
    "source" => "universal",  
    "url"    => "https://sandbox.example.com/",  
];  

$session = curl_init();  

curl_setopt($session, CURLOPT_URL, $url);  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($params));  
curl_setopt($session, CURLOPT_POST, true);  
curl_setopt($session, CURLOPT_USERPWD, $username . ":" . $password);  
curl_setopt($session, CURLOPT_HTTPHEADER, [  
    "Content-Type: application/json",  
]);

$response = curl_exec($session);

if (curl_errno($session)) {  
    echo 'cURL Error: ' . curl_error($session);  
} else {  
    echo "Response: \n";  
    echo $response;  
}

curl_close($session);  
?>  
Enter fullscreen mode Exit fullscreen mode

This will send a POST request and retrieve the data for you. The response will be in JSON format, making it easy to parse and manipulate.

Step 6: Storing Your Data in CSV Format

Once you’ve pulled the data, you’ll probably want to save it for later use. Saving the response to a CSV file is a simple way to handle it:

function saveResponseToCSV(array $data, string $filename)  
{  
    $file = fopen($filename, 'w');  
    if (!$file) {  
        echo "Failed to open file: $filename\n";  
        return;  
    }

    // Write header  
    fputcsv($file, array_keys($data["results"][0]));  

    // Write data rows  
    foreach ($data["results"] as $row) {  
        fputcsv($file, $row);  
    }

    fclose($file);  
}  
Enter fullscreen mode Exit fullscreen mode

After fetching the response, you can decode the JSON and pass the data to this function to save it in a data.csv file.

Conclusion

At this point, you should have a solid understanding of how to use cURL in PHP, from making simple GET requests to handling authentication, proxies, and integrating with API. With these skills, you’re ready to tackle most web scraping tasks. The next step is to integrate these methods into your projects and enhance your web data handling capabilities.

Top comments (0)