开发者

Help convert .asp to .php

开发者 https://www.devze.com 2023-01-31 17:41 出处:网络
How can I rewrite this from .asp to .php? <% \' Url of the webpage we want to retrieve thisURL = \"pagerequest.jsp\"

How can I rewrite this from .asp to .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 U开发者_JAVA百科RL
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

%>


How? What you need to do is learn PHP and then write it.

If you already know PHP, then I suspect you'll want to investigate:

  1. Whether or not you have remote file_get_contents support. (See the other answers.)

  2. Failing that, whether or not you can use the CURL functions, although you should first check that your production environment has curl support. (If it doesn't, you'll need to rectify this.)

  3. Failing all of those, you'll need to create a socket connection and send the relevant HTTP headers to request the remote content.

Of the above, I'd almost recommend CURL above file_get_contents, as it can transparently handle re-directs (if you tell it to) and will expose more of the under-pinnings, which may prove useful in the future.


Well, the translated code is (no error checking, just dirt simple functionality):

$url = 'http://server/pagerequest.jsp';
$text = file_get_contents($url);
header('Content-Type: text/xml');
echo $text;

Note that the $url needs to be fully qualified...

EDIT: for a more robust solution:

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;
    }
 }

Usage:

$url = 'http://server/pagerequest.jsp';
$text = getUrl($url);
header('Content-Type: text/xml');
echo $text;
0

精彩评论

暂无评论...
验证码 换一张
取 消