Skip to content

Instantly share code, notes, and snippets.

@shengjie
Last active August 29, 2015 14:27
Show Gist options
  • Save shengjie/fc8a6fa1e65536ba7a3e to your computer and use it in GitHub Desktop.
Save shengjie/fc8a6fa1e65536ba7a3e to your computer and use it in GitHub Desktop.
A simple solution for captureable redirect response. browser can handle this response easily
<?php
use Symfony\Component\HttpFoundation\Response;
class MetaRedirectResponse extends Response
{
/**
* Constructor.
*
* @param mixed $content The response content, see setContent()
* @param int $status The response status code
* @param array $headers An array of response headers
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*
* @api
*/
public function __construct($url, $headers = array())
{
parent::__construct(sprintf(
'<html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=%1$s" /><title>Redirecting to %1$s</title></head><body>Redirecting to <a href="%1$s">%1$s</a>.<script type="text/javascript">location.href="%1$s";</script></body></html>', $url)
, 200,
$headers
);
}
}
@shengjie
Copy link
Author

Using following code to capture meta redirect response.

function findMediaRedirect(data) {
    var regex = /<meta\s+http-equiv=["']refresh["']\s+content=["']\d+;url=([^"']+)["']/i;
    var found = data.match(regex);

    if (found[1]) return found[1];
    return null;
}

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