"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Introduction to PHP cURL and REST APIs

 

Modern web applications often need to communicate with other applications. For example:

  • A website might fetch weather data from an API.
  • An e-commerce store might connect to PayPal or Stripe for payments.
  • A blog might display tweets using the Twitter API.

This is where REST APIs and cURL in PHP come in.


What is a REST API?

REST (Representational State Transfer) is a common way for applications to talk to each other over the internet.

  • Uses HTTP methods like:
    • GET → Retrieve data
    • POST → Send data
    • PUT → Update data
    • DELETE → Remove data
  • Data is usually sent/received in JSON format.

👉 Example: A weather API might give data like this:

{

  "city": "Dubai",

  "temperature": "39°C",

  "condition": "Sunny"

}


What is cURL in PHP?

cURL (Client URL) is a PHP library that allows you to send HTTP requests (GET, POST, PUT, DELETE) to APIs or web pages and receive responses.

It’s the most common way to interact with REST APIs in PHP.


📝 Example 1: GET Request with cURL

<?php

$url = "https://jsonplaceholder.typicode.com/posts/1";

 

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

$response = curl_exec($ch);

curl_close($ch);

 

$data = json_decode($response, true);

 

echo "Title: " . $data['title'];

?>

This fetches a post from a fake JSON API and prints its title.


📝 Example 2: POST Request with cURL

<?php

$url = "https://jsonplaceholder.typicode.com/posts";

 

$postData = [

    "title" => "My Blog Post",

    "body" => "This is an example post.",

    "userId" => 1

];

 

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

curl_setopt($ch, CURLOPT_HTTPHEADER, [

    'Content-Type: application/json'

]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

$response = curl_exec($ch);

curl_close($ch);

 

$data = json_decode($response, true);

 

echo "New Post ID: " . $data['id'];

?>

This sends data to the API and creates a new resource.


📝 Example 3: Handling Headers and Authentication

Many APIs require API keys or authentication tokens.

<?php

$url = "https://api.example.com/user";

 

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HTTPHEADER, [

    'Authorization: Bearer YOUR_API_KEY_HERE',

    'Content-Type: application/json'

]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

$response = curl_exec($ch);

curl_close($ch);

 

$data = json_decode($response, true);

print_r($data);

?>


🎯 Best Practices

  • Always check API responses and handle errors.
  • Use json_decode() to work with JSON data.
  • Keep API keys secure (use .env files).
  • Use try...catch or error checks for failed requests.

👉 Conclusion

 

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)