Back to blog

How to Use cURL in PHP With Examples

Maryia Stsiopkina

2025-02-195 min read
Share

For any developer, data scientist, or anyone who might need to get data quickly from the web, client URL (cURL) and PHP's cURL functions are indispensable.Various tools, including PHP cURL converter, can help developers translate between different request formats, but understanding how to use cURL directly in PHP is essential. In the context of PHP, cURL allows for efficient HTTP requests and data manipulation, whether working with APIs, performing basic HTTP authentication, or web scraping tasks. In this tutorial, we'll take a look at how to use cURL in PHP programs to access data on the web with the help of Oxylabs proxies and Web Scraper API.

Preparing the environment

First, let's set up the environment needed to run PHP code. If you don't have it already, you'll need to install PHP on your system. 

For MacOS and Linux-based machines, you can use the local package manager. The command line tool would look like this:

brew install php

For Windows, you’d need to visit PHP’s official download page here.

After that’s complete, let’s create a file called index.php in your selected directory and run this command to start the server.

php -S localhost:8000

Your PHP server should be up and running. 

We’ll need to make sure that cURL is installed as well. We can do that by pasting this simple script in the created file:

<?php
  phpinfo();
?>

Save it and go to http://localhost:8000 in your browser and try searching for curl

You will see the following:

If you don’t see it, you’ll need to enable cURL in your PHP configuration file, which can be located by running this command:

php --ini

The output should contain a line that points to the path of the `php.ini` file. On MacOS, it will look like this: /opt/homebrew/etc/php/8.4/php.ini 

Open that and look for the line extension=curl or extensions=php_curl. If you uncomment that line and restart the PHP server, you should see cURL enabled in your PHP configuration. 

Basic syntax and structure

There are a few basic steps that you need to do to create a predefined cURL session and handle the proper URL syntax:

  • Initialize a session

  • Set options on the session object

  • Execute the session with the set options.

Here’s an example of setting up a session with cURL in PHP to perform a GET request. This simple cURL script demonstrates the basic structure you'll need for any request type. We’ll use https://ip.oxylabs.io/ as the URL to get our current IP address.

<?php
    $url = "https://ip.oxylabs.io/";

    // Initialize cURL session
    $session = curl_init();

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


    if (curl_errno($session)) { // Check for errors
        echo 'cURL Error: ' . curl_error($session);
    } else {
        echo "Response: \n"; // If no errors, print out the response.
        echo $response;
    }
    
    curl_close($session); // Close the session.
?>

Then, if you open http://localhost:8000 , you should see a response with your IP address in it.

Authentication

Let’s look at an example where we would need to use some basic authentication to reach an endpoint with cURL. 

To set user credentials, we can use the CURLOPT_USERPWD option on the session, as well as the CURLOPT_HTTPAUTH to CURLAUTH_BASIC to indicate that we’ll be using basic authentication.

Here’s an example of how it can look like on the same code used before:

<?php
    $url = "https://ip.oxylabs.io/";

    // Initialize cURL session
    $session = curl_init();

    curl_setopt($session, CURLOPT_URL, $url);            // Set the URL
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
    curl_setopt($ch, CURLOPT_USERPWD, "user:passwd"); // Set username:password for Basic Auth
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // Use Basic Authentication
    $response = curl_exec($session); // Execute the session


    if (curl_errno($session)) { // Check for errors
        echo 'cURL Error: ' . curl_error($session);
    } else {
        echo "Response: \n"; // If no errors, print out the response.
        echo $response;
    }
    
    curl_close($session); // Close the session.
?>


Using Oxylabs proxies with cURL

If we aim to access some content on the internet with cURL through a proxy, some additional steps are needed to make it possible. Usually, you’ll need to authenticate with the proxy first before being able to use it. Also, here’s a tutorial if you need some extra help setting up proxies in PHP

Secondly, you’ll need to specify the host or IP address of the proxy server through which you want to route your cURL request.

Let’s use the proxies provided by Oxylabs for this demonstration. Open the same file as before and add this line after the URL variable. It should contain the hostname or IP address of the proxy you wish to use.

$proxy = 'pr.oxylabs.io:7777';

After that, let’s define the credentials as separate variables as well. Replace the placeholders with your own Oxylabs proxy credentials.

$username = 'USERNAME';
$password = 'PASSWORD';

Once the proxy host and credentials are defined, let’s add them as options to our cURL session.

It should look like this:

curl_setopt($session, CURLOPT_PROXY, "http://$proxy");  // Set the proxy
curl_setopt($session, CURLOPT_PROXYUSERPWD, "customer-$username:$password");
// Set the proxy credentials

Here’s the full code to use proxies with cURL in PHP:

<?php

$url      = "https://ip.oxylabs.io/";
$username = "USERNAME";
$password = "PASSWORD";
$proxy    = 'pr.oxylabs.io:7777';
// Initialize cURL session
$session = curl_init();

curl_setopt($session, CURLOPT_URL, $url); // Set the URL
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
curl_setopt($session, CURLOPT_PROXY, "http://$proxy"); // Set the proxy
curl_setopt($session, CURLOPT_PROXYUSERPWD, "customer-$username:$password"); // Set the proxy credentials
$response = curl_exec($session); // Execute the session

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

curl_close($session); // Close the session.

?>

Using Oxylabs Web Scraper API

Another common use case for cURL is accessing Oxylabs APIs in your PHP app. Let’s imagine we’re writing a simple application to scrape data from any publicly available website.

The best tool to use in this case would be Oxylabs Web Scraper API, as it makes it possible to gather data from any publicly available source, such as search engines, e-commerce sites, and many more. You can read more about it here.

If you’d like to test the Web Scraper API, you can register for a 7-day free trial in the Oxylabs dashboard.

Let’s try sending a sample request to the Web Scraper API using cURL and PHP. 

Start by defining the URL of the API and the parameters you wish to use. In this example, we’ll be using the Oxylabs sandbox as a target. Also, define the username and password variables that should be your API credentials as well.

It should look like this:

$url    = "https://realtime.oxylabs.io/v1/queries";

$username = "USERNAME";
$password = "PASSWORD";

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

Next, let’s define the session and set its options, similar to what we did before. However, this time, we’ll be sending HTTP POST requests with the defined parameters encoded in its body.
To do that, we just need to use a few more curl_setopt commands, including setting specific headers and basic authentication for our POST request.

It can look like this:

$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",
]);

The rest of the code should match what we did before. 

Here’s what it should look like:

<?php
$url = "https://realtime.oxylabs.io/v1/queries";

$username = "USERNAME";
$password = "PASSWORD";

$params = [
    "source" => "universal",
    "url"    => "https://sandbox.oxylabs.io/products/1",
];

$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);

?>

As a response, you should see the raw data returned from the Oxylabs Web Scraper API. It should look like this:

{
    "results": [
        {
            "content": "<!DOCTYPE html><html lang=\"en\">
            CONTENT
            </html>",
            "created_at": "2025-02-13 12:00:14",
            "updated_at": "2025-02-13 12:00:15",
            "page": 1,
            "url": "https://sandbox.oxylabs.io/products/1",
            "job_id": "7213505427934323153",
            "status_code": 200
        }
    ]
}

Saving data from the API to a CSV file

Once we retrieve data from the scraped website, we’d likely want to store it somewhere for further use. A simple example would be a CSV file. 
Let’s write a separate function called saveResultsToCSV to store the retrieved results in a CSV file. The function should accept a data argument with type array, and a filename argument of type string.

function saveResponseToCSV(array $data, string $filename)
{
}

Before implementing the function, let's decode our JSON data into a format that PHP can work with and call our function with the result.

Here’s how it can look:

$response = curl_exec($session);

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

    if ($data) {
        saveResponseToCSV($data, 'response.csv');
        echo "Response saved to response.csv\n";
    } else {
        echo "Failed to parse response.\n";
    }

    echo $response;
}

Now we can implement the saveResponseToCSV function. It should consist of four parts:

  • Opening the specified file

  • Writing the header row to the CSV file.

  • Writing each result entry to the CSV file.

  • Closing the file.

Here’s what it should look like, based on the expected response from Oxylabs’ Web Scraper API:

function saveResponseToCSV(array $data, string $filename)
{
    $file = fopen($filename, 'w');

    if (! $file) {
        echo "Failed to open file: $filename\n";
        return;
    }

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

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

    fclose($file);

}

After combining it all together, the full code should look like this:

<?php
$url = "https://realtime.oxylabs.io/v1/queries";
$username = "USERNAME";
$password = "PASSWORD";

$params = [
    'source' => 'universal',
    'url'    => 'https://sandbox.oxylabs.io/products/1
',
];

$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";
    $data = json_decode($response, true);

    if ($data) {
        saveResponseToCSV($data, 'data.csv');
        echo "Response saved to data.csv\n";
    } else {
        echo "Failed to parse response.\n";
    }

    echo $response;
}

curl_close($session);

function saveResponseToCSV(array $data, string $filename)
{
    $file = fopen($filename, 'w');

    if (! $file) {
        echo "Failed to open file: $filename\n";
        return;
    }
    $results = $data["results"];

    fputcsv($file, array_keys($results[0]));

    foreach ($results as $row) {
        fputcsv($file, $row);
    }
    fclose($file);
}

After running the code, you should see a file named data.csv in your project directory. If you open it, here’s what you’ll see:

Conclusion

In this tutorial, we covered the basic principles of using cURL in PHP, including simple GET calls, authenticating with credentials, using proxies, and retrieving data from Oxylabs’ Web Scraper API. These steps should be helpful in sending requests and retrieving data in PHP in an easy and understandable way.

Frequently Asked Questions

What is cURL in PHP?

cURL in PHP is a library that enables making HTTP requests and transferring data between servers. It's widely used for interacting with APIs, fetching web content, and performing web scraping tasks.

How to run PHP cURL?

Running PHP cURL involves initializing a session, configuring request options (like URL, headers, and authentication), executing the request to get a response, and then closing the session. These steps create a complete request-response cycle.

Is cURL built in PHP?

Not by default. While PHP often includes cURL support, it is a separate extension that needs to be installed and enabled. Many PHP installations come with cURL pre-installed, but you may need to install it separately or enable it in your PHP configuration if it's not already active.

What is the alternative to cURL in PHP?

PHP offers several alternatives to cURL for making HTTP requests. These include native PHP functions for file operations, modern HTTP client libraries, and networking tools. While these alternatives might be suitable for basic requests, cURL remains the most feature-rich solution for complex HTTP operations.

About the author

Maryia Stsiopkina avatar

Maryia Stsiopkina

Senior Content Manager

Maryia Stsiopkina is a Senior Content Manager at Oxylabs. As her passion for writing was developing, she was writing either creepy detective stories or fairy tales at different points in time. Eventually, she found herself in the tech wonderland with numerous hidden corners to explore. At leisure, she does birdwatching with binoculars (some people mistake it for stalking), makes flower jewelry, and eats pickles.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I’m interested