{"id":853,"date":"2021-03-06T10:26:54","date_gmt":"2021-03-06T02:26:54","guid":{"rendered":"http:\/\/www.tra56.com\/?p=853"},"modified":"2021-03-06T10:27:09","modified_gmt":"2021-03-06T02:27:09","slug":"853","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/853.html","title":{"rendered":"php\u83b7\u53d6\u591a\u6b21\u8df3\u8f6c\u540e\u771f\u5b9e\u7684url\u5730\u5740"},"content":{"rendered":"
<?php\r\n\/*\r\n    \u83b7\u53d6\u591a\u6b21\u8df3\u8f6c\u540e\u771f\u5b9e\u7684url\r\n    @param str $url \u67e5\u8be2\r\n    $return str  \u5b9a\u5411\u540e\u7684url\u7684\u771f\u5b9eurl\r\n *\/\r\nfunction getrealurl($url){\r\n    $header = @get_headers($url,1);  \/\/\u9ed8\u8ba4\u7b2c\u4e8c\u4e2a\u53c2\u65700\uff0c\u53ef\u90091\uff0c\u8fd4\u56de\u5173\u8054\u6570\u7ec4\r\n    if(!$header){\r\n        exit('\u65e0\u6cd5\u6253\u5f00\u6b64\u7f51\u7ad9'.$url);\r\n    }\r\n    \/\/var_dump($header);\r\n    if (strpos($header[0],'301') || strpos($header[0],'302')) {\r\n        if(is_array($header['Location'])) {\r\n            return $header['Location'][count($header['Location'])-1];\r\n        }else{\r\n            return $header['Location'];\r\n        }\r\n    }else {\r\n        return $url;\r\n    }\r\n}\r\n \r\n$url = 'https:\/\/git.io\/JtLZy';\r\n$url = getrealurl($url);\r\necho '\u771f\u5b9e\u7684url\u4e3a\uff1a'.$url;<\/pre>\n
<?php\r\n\/*\r\n    \u83b7\u53d6\u591a\u6b21\u8df3\u8f6c\u540e\u771f\u5b9e\u7684url\r\n    @param str $url \u67e5\u8be2\r\n    $return str  \u5b9a\u5411\u540e\u7684url\u7684\u771f\u5b9eurl\r\n *\/\r\nfunction getrealurl($url){\r\n    $header = @get_headers($url,1);  \/\/\u9ed8\u8ba4\u7b2c\u4e8c\u4e2a\u53c2\u65700\uff0c\u53ef\u90091\uff0c\u8fd4\u56de\u5173\u8054\u6570\u7ec4\r\n    if(!$header){\r\n        exit('\u65e0\u6cd5\u6253\u5f00\u6b64\u7f51\u7ad9'.$url);\r\n    }\r\n    \/\/var_dump($header);\r\n    if (strpos($header[0],'301') || strpos($header[0],'302')) {\r\n        if(is_array($header['Location'])) {\r\n            return $header['Location'][count($header['Location'])-1];\r\n        }else{\r\n            return $header['Location'];\r\n        }\r\n    }else {\r\n        return $url;\r\n    }\r\n}\r\n \r\n$url = 'https:\/\/git.io\/JtLZy';\r\n$url = getrealurl($url);\r\necho '\u771f\u5b9e\u7684url\u4e3a\uff1a'.$url;<\/pre>\n

\u901a\u8fc7PHP\u7684get_headers()\u51fd\u6570\u6765\u83b7\u53d6\u8df3\u8f6c\u540e\u7684\u7f51\u5740<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

<?php \/*     \u83b7\u53d6\u591a\u6b21\u8df3\u8f6c\u540e\u771f\u5b9e\u7684url &nbsp […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[26,72],"class_list":["post-853","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/853"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/853\/revisions\/854"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}