PHP Script to follow HTTP Redirects
I'm looking for a way to write a PHP script which can be given a URL to a URL shortener site such as tinyurl, then follow the HTTP redirect and return the URL it points to. I've written some PHP + MySQl scripts before, but this will be 开发者_运维百科my first foray into accessing remote page metadata using PHP and I'm hoping someone can point me in the right direction and save me fumbling around tutorials looking for tools which may not exist!
You might want to take a look at PHP cURL. It might depend on the redirect type as to how you employ it.
the curl extension can do it.
list of options here
http://www.php.net/manual/en/function.curl-setopt.php
you want
CURLOPT_FOLLOWLOCATION
then use
http://www.php.net/manual/en/function.curl-getinfo.php
CURLINFO_EFFECTIVE_URL
You can probably do the same with php's regular filesyst6em and stream functions(eg, file_get_contents or fopen etc...) I know they follow redirects, but I don't know off hand how to get the effective url, although I think I remember seeing it done.
http://docs.php.net/manual/en/ref.curl.php#93163
精彩评论