Skip to content

Instantly share code, notes, and snippets.

@m3h3d1ha2an
Created October 24, 2025 16:19
Show Gist options
  • Select an option

  • Save m3h3d1ha2an/98f5f1535f2c14aa7dce447acfe6e67d to your computer and use it in GitHub Desktop.

Select an option

Save m3h3d1ha2an/98f5f1535f2c14aa7dce447acfe6e67d to your computer and use it in GitHub Desktop.
<?php
<!-- input from anywhere (browser, mobile app, postman, etc) -->
<!-- /api/v1/blogs/42/comments/7?sort=desc&limit=5&include=author,tags -->

<!-- output -->
 <!-- {
  "full_url": "/api/v1/blogs/42/comments/7?sort=desc&limit=5&include=author,tags",
  "path_segments": ["api", "v1", "blogs", "42", "comments", "7"],
  "resource": "blogs",
  "resource_id": "42",
  "sub_resource": "comments",
  "sub_resource_id": "7",
  "query_params": {
    "sort": "desc",
    "limit": "5",
    "include": "author,tags"
  }
} -->



// header("content-type:application/json");
// $url = $_SERVER["REQUEST_URI"];

// $params = parse_url($url, PHP_URL_PATH);

// $splited_url = explode("/", trim($params, "/"));

// echo $solve = json_encode([

//     "full_url"        => $url,
//     "path_segments"   => $splited_url,
//     "resource"        => $splited_url[2],
//     "resource_id"     => $splited_url[3],
//     "sub_resource"    => $splited_url[4],
//     "sub_resource_id" => $splited_url[5],
//     "query_params"    => $_GET,
// ]);



header("Content-Type: application/json; charset=utf-8");

// পুরো URL
$full_url = $_SERVER["REQUEST_URI"];

// path আলাদা করা (query parameter ছাড়া)
$path = parse_url($full_url, PHP_URL_PATH);

// path কে segment এ ভাগ করা
$segments = explode("/", trim($path, "/"));

// নিরাপদভাবে segment access করা
$resource        = isset($segments[2]) ? $segments[2] : null;
$resource_id     = isset($segments[3]) ? $segments[3] : null;
$sub_resource    = isset($segments[4]) ? $segments[4] : null;
$sub_resource_id = isset($segments[5]) ? $segments[5] : null;

// output JSON
echo json_encode([
    "full_url"        => $full_url,
    "path_segments"   => $segments,
    "resource"        => $resource,
    "resource_id"     => $resource_id,
    "sub_resource"    => $sub_resource,
    "sub_resource_id" => $sub_resource_id,
    "query_params"    => $_GET
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment