Skip to content

Instantly share code, notes, and snippets.

@m3h3d1ha2an
Last active October 24, 2025 16:14
Show Gist options
  • Select an option

  • Save m3h3d1ha2an/680b0fec4efab4ed4459d3f867f2d54f to your computer and use it in GitHub Desktop.

Select an option

Save m3h3d1ha2an/680b0fec4efab4ed4459d3f867f2d54f to your computer and use it in GitHub Desktop.

নিশ্চয়! যেহেতু আপনি PHP এবং REST API style URL handling নিয়ে কাজ করছেন, আমি আপনাকে ৫টি প্র্যাকটিস টাস্ক দিচ্ছি যা real-world API parsing ও PHP array/json handling-এ সাহায্য করবে।


1. Dynamic Resource Extraction

Task: একটি URL string নিয়ে লিখুন যে function:

  • সব resource এবং sub-resource কে dynamically array আকারে বের করবে।
  • যদি resource বা sub-resource না থাকে, তাহলে null রাখবে।

Example URL:

/api/v1/users/15/posts/7/comments/3

Expected Output:

{
  "resources": [
    {"name":"users","id":"15"},
    {"name":"posts","id":"7"},
    {"name":"comments","id":"3"}
  ]
}

2. Query Parameters Parser

Task: একটি function লিখুন যা URL থেকে query parameters আলাদা করবে এবং:

  • যদি numeric value থাকে, সেটিকে integer হিসেবে রাখবে।
  • যদি comma-separated list থাকে, সেটাকে array এ রূপান্তর করবে।

Example URL:

/api/v1/products?category=books,electronics&limit=10

Expected Output:

{
  "category": ["books", "electronics"],
  "limit": 10
}

3. Validate Path Segments

Task: URL path validate করার জন্য একটি function লিখুন।

  • Resource name শুধু alphabetic হোক।
  • Resource ID numeric হোক।
  • Invalid হলে error message return করবে।

Example:

/api/v1/blogs/abc/comments/7

→ error, কারণ blogs ঠিক আছে কিন্তু abc numeric নয়।


4. Full URL Builder

Task: একটি function লিখুন যা:

  • base URL + path segments + query parameters → পূর্ণ URL তৈরি করবে।
  • Flag দিয়ে decide করতে পারবে / escape করবে কি না (JSON_UNESCAPED_SLASHES)।

Input:

$base = "/api/v1";
$segments = ["blogs", "42", "comments"];
$query = ["sort"=>"desc", "limit"=>5];

Output:

/api/v1/blogs/42/comments?sort=desc&limit=5

5. Log API Access in Terminal

Task: লিখুন একটি PHP script যা:

  • কোনো API request এ incoming URL, path segments, query parameters terminal/console এ print করবে।
  • Browser-এ JSON output দেয়।
  • Debugging এর জন্য error_log() ব্যবহার করা।

Example:

Terminal Output:
Accessed URL: /api/v1/users/12?include=profile
Segments: ["api","v1","users","12"]
Query: ["include"=>"profile"]

💡 এগুলো practice করলে আপনি URL parsing, JSON handling, query param parsing, validation এবং debugging-এ অনেক দক্ষ হয়ে যাবেন।


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment