Get closest page ID from URL

You should be able to get the current url from $url = add_query_arg().

Then assuming, the structure: www.example.com/page/subpage/news/ID/post you can use preg_match to extract /page/subpage/news. Then it would be simple matter of using get_page_by_path();.

If that fails you can then check /page/subpage/ and finally /page.