我怎样才能将.asp重写为.php?
<%
' Url of the webpage we want to retrieve
thisURL = "pagerequest.jsp"
' Creation of the xmlHTTP object
Set GetConnection = CreateObject("Microsoft.XMLHTTP")
' Connection to the URL
GetConnection.Open "get", thisURL, False
GetConnection.Send
' ResponsePage now have the response of the remote web server
ResponsePage = GetConnection.responseText
' We write out now the content of the ResponsePage var
Response.ContentType = "text/xml"
Response.write (ResponsePage)
Set GetConnection = Nothing
Run Code Online (Sandbox Code Playgroud)
%>
好吧,翻译的代码是(没有错误检查,只是简单的功能):
$url = 'http://server/pagerequest.jsp';
$text = file_get_contents($url);
header('Content-Type: text/xml');
echo $text;
Run Code Online (Sandbox Code Playgroud)
请注意,$url需要完全合格......
编辑:更强大的解决方案:
function getUrl($url) {
if (ini_get('allow_url_fopen')) {
return file_get_contents($url);
} elseif (function_exists('curl_init')) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
return curl_exec($c);
} else {
$parts = parse_url($url);
if (!isset($parts['host'])) {
throw new Exception('You need a host!');
}
$port = isset($parts['port']) ? $parts['port'] : 80;
$f = fsockopen($parts['host'], $port, $errno, $errstr, 30);
if (!$f) {
throw new Exception('Error: ['.$errno.'] '.$errstr);
}
$out = "GET $url HTTP/1.1\r\n";
$out .= "Host: {$parts['host']}\r\n";
$out .= "Connection: close\r\n\r\n";
fwrite($f, $out);
$data = '';
while (!feof($f)) {
$data .= fgets($f, 128);
}
list($headers, $data) = explode("\r\n\r\n", $data, 2);
// Do some validation on the headers to check for redirect/error
return $data;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
$url = 'http://server/pagerequest.jsp';
$text = getUrl($url);
header('Content-Type: text/xml');
echo $text;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |