0

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;
}
echo(getheader($_GET['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 
x.Send()
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!

afazolo
  • 382
  • 2
  • 6
  • 22
  • 1
    Why do you need to call PHP at all? `x.getResponseHeader("location")` or something like that should be available for MSXML2.ServerXMLHTTP – Viktor S. 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 03: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: http://stackoverflow.com/questions/161343/how-do-i-prevent-serverxmlhttp-from-automatically-following-redirects-http-303 There you can find an object which allows you to prevent redirects. And suppose it also has some method like `getResponseHeader` – Viktor S. Jan 16 '13 at 07:55
  • Here is a method list for that object: http://msdn.microsoft.com/ru-ru/library/windows/desktop/aa384106(v=vs.85).aspx#methods And as I can see, there is a GEtResponseHeader method – Viktor S. Jan 16 '13 at 07:57
  • You can put it as an answer for future visitors. – Viktor S. Jan 16 '13 at 11:46

1 Answers1

1

Here's the solution:

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

Thanks to FAngel for your replies!

afazolo
  • 382
  • 2
  • 6
  • 22