帮助将.asp转换为.php

det*_*ate 0 php asp-classic

我怎样才能将.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)

%>

Joh*_*ker 6

怎么样?你需要做的是学习PHP,然后编写它.

如果您已经了解PHP,那么我怀疑您需要调查:

  1. 是否具有远程file_get_contents支持.(见其他答案.)

  2. 如果不这样做,您是否可以使用CURL功能,尽管您应该首先检查您的生产环境是否有卷曲支持.(如果没有,你需要纠正这个问题.)

  3. 如果没有完成所有这些操作,您将需要创建套接字连接并发送相关的HTTP标头以请求远程内容.

在上面,我几乎推荐CURL高于file_get_contents,因为它可以透明地处理重定向(如果你告诉它)并且会暴露更多的欠固定,这可能在将来证明是有用的.


irc*_*ell 5

好吧,翻译的代码是(没有错误检查,只是简单的功能):

$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)