Real-world API integration (payment, weather, AI)

🚀 1️⃣ Payment API Integration (Razorpay Example)

🔹 Use Case:

Accept online payments on your website.


🔹 Step 1: Install Razorpay SDK

composer require razorpay/razorpay

🔹 Step 2: Create Order (Backend PHP)

<?php
require 'vendor/autoload.php';use Razorpay\Api\Api;$api = new Api("YOUR_KEY_ID", "YOUR_SECRET");$order = $api->order->create([
'receipt' => 'order_rcptid_11',
'amount' => 50000, // amount in paise (₹500)
'currency' => 'INR'
]);echo json_encode($order);
?>

🔹 Step 3: Checkout Button (Frontend)

<script src="https://checkout.razorpay.com/v1/checkout.js"></script><button id="payBtn">Pay Now</button><script>
document.getElementById('payBtn').onclick = function () {
var options = {
key: "YOUR_KEY_ID",
amount: "50000",
currency: "INR",
name: "SOA Technology",
handler: function (response){
alert("Payment Successful: " + response.razorpay_payment_id);
}
};
var rzp = new Razorpay(options);
rzp.open();
}
</script>

🌦️ 2️⃣ Weather API Integration (OpenWeather)

🔹 Use Case:

Show live weather on website/app.


🔹 Step 1: API URL

https://api.openweathermap.org/data/2.5/weather?q=Delhi&appid=YOUR_API_KEY&units=metric

🔹 Step 2: PHP Integration

<?php
$city = "Delhi";
$apiKey = "YOUR_API_KEY";$url = "https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey&units=metric";$response = file_get_contents($url);$data = json_decode($response, true);echo "City: " . $data['name'] . "<br>";
echo "Temperature: " . $data['main']['temp'] . "°C<br>";
echo "Weather: " . $data['weather'][0]['description'];
?>

🔹 Output Example:

City: Delhi
Temperature: 32°C
Weather: clear sky

🤖 3️⃣ AI API Integration (OpenAI Example)

🔹 Use Case:

Chatbot, content generator, AI tools (like your website)


🔹 Step 1: cURL Request

<?php
$apiKey = "YOUR_OPENAI_API_KEY";$data = [
"model" => "gpt-4.1-mini",
"messages" => [
["role" => "user", "content" => "Explain business ideas"]
]
];$ch = curl_init("https://api.openai.com/v1/chat/completions");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer $apiKey"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));$response = curl_exec($ch);
curl_close($ch);$result = json_decode($response, true);echo $result['choices'][0]['message']['content'];
?>

⚡ Real Project Ideas Using These APIs

💡 Combine APIs for powerful apps:

  • 🛒 E-commerce + Razorpay → Payment system
  • 🌍 Travel App + Weather API → Show destination weather
  • 🤖 AI Blog Generator → Auto content for SEO (perfect for your site)
  • 🧿 Spiritual AI Tool → AI + random logic (your current project 😉)

🔐 Important Best Practices

  • Never expose API keys in frontend
  • Use .env file for secrets
  • Add error handling:
if(!$response){
echo "API Error";
}
  • Validate user input (security)

🎯 What You Learn

  • Real-world backend skills
  • API request/response handling
  • JSON parsing
  • Building SaaS tools