Take the 2-minute tour ×

I have a php + curl function that captures the "HTTP 301" responses. The page is named get_header.php and is located in the folder /exe:

function getheader($url)
// Use curl to fetch the HTTP Headers
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1); // just the header
curl_setopt($ch, CURLOPT_NOBODY, 1); // not the body
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
preg_match('/Location: (.*)\n/', $output, $matches);
// if no redirect header then return the original url
return isset($matches[1]) ? $matches[1] : $url;

However, I need to use this script in a classic asp page. I tried to call it via HTTP:

Dim x: Set x = CreateObject("MSXML2.ServerXMLHTTP")     
Dim y: y = "http://mysite.com/exe/get_header.php?url=" & url
x.Open "GET", y, false 
Dim z: z = x.ResponseText

Although it works, this is not the ideal solution since both pages are on the same domain. In fact it has generated slowness of requests. So how could I solve this problem? The ideal solution would be a vbscript or javascript version of my PHP function. Thanks!

share|improve this question
Why do you need to call PHP at all? x.getResponseHeader("location") or something like that should be available for MSXML2.ServerXMLHTTP –  FAngel Jan 15 '13 at 17:59
Thanks for your reply, but with the PHP function I get the destination of $_GET['url'] value. How would I do this using your suggestion? –  afazolo Jan 15 '13 at 21:53
I verified that there is no "location" header for MSXML2.ServerXMLHTTP or MSXML2.ServerXMLHTTP.6.0. or Microsoft.XMLHTTP. –  afazolo Jan 16 '13 at 3:08
Suppose that is because it goes to that location. Actually, do a redirect. Which does not happen when you send request with curl (because of this line:curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); ) See this question: stackoverflow.com/questions/161343/… There you can find an object which allows you to prevent redirects. And suppose it also has some method like getResponseHeader –  FAngel Jan 16 '13 at 7:55
Here is a method list for that object: msdn.microsoft.com/ru-ru/library/windows/desktop/… And as I can see, there is a GEtResponseHeader method –  FAngel Jan 16 '13 at 7:57

1 Answer 1

up vote 1 down vote accepted

Here's the solution:

Dim x: Set x = CreateObject("WinHttp.WinHttpRequest.5.1")
x.Option(6) = False
x.Open "GET", url, false
If x.status = 301 Then
Dim z: z = x.getResponseHeader("Location")
End If

Thanks to FAngel for your replies!

share|improve this answer

Your Answer


