【发布时间】:2012-12-29 14:38:15
【问题描述】:
我有一个捕获“HTTP 301”响应的 php + curl 函数。该页面名为get_header.php,位于文件夹/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']));
但是,我需要在经典的 asp 页面中使用这个脚本。我尝试通过 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
虽然它有效,但这不是理想的解决方案,因为两个页面都在同一个域中。事实上,它已经导致请求变慢。那么我该如何解决这个问题呢?理想的解决方案是我的 PHP 函数的 vbscript 或 javascript 版本。谢谢!
【问题讨论】:
-
为什么需要调用 PHP?
x.getResponseHeader("location")或类似的东西应该可用于 MSXML2.ServerXMLHTTP -
感谢您的回复,但是通过 PHP 函数,我得到了 $_GET['url'] 值的目的地。我将如何使用您的建议来做到这一点?
-
我验证了 MSXML2.ServerXMLHTTP 或 MSXML2.ServerXMLHTTP.6.0 没有“位置”标头。或 Microsoft.XMLHTTP。
-
假设是因为它去了那个位置。实际上,做一个重定向。当您使用 curl 发送请求时不会发生这种情况(因为此行:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);)请参阅此问题:stackoverflow.com/questions/161343/… 在那里您可以找到一个允许您防止重定向的对象。并假设它也有一些方法,如getResponseHeader -
这是该对象的方法列表:msdn.microsoft.com/ru-ru/library/windows/desktop/… 正如我所见,有一个 GEtResponseHeader 方法
标签: php curl asp-classic xmlhttprequest