VBScript file_get_contents function

A VBScript equivalent of PHP’s file_get_contents

Public function file_get_contents(filename)

    Dim ts
    Dim contents

    if left(filename,7) <> "http://" and file_exists( filename ) then
        Set TS = fso.OpenTextFile( fileMapPath(filename),1)

        If TS.AtEndOfStream <> True Then
           contents = TS.ReadAll
        End If

        file_get_contents = contents
        Exit Function
    end if

    if left(filename,7) <> "http://" then
        file_get_contents = false
        Exit Function
    end if

    Dim objWinHttp
    'Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
    'Set objWinHttp = Server.CreateObject("MSXML2.XMLHTTP")
    Set objWinHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")

    objWinHttp.Open "GET", filename, false
    objWinHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    'Response.Write objWinHttp.Status & " " & objWinHttp.StatusText

    file_get_contents = objWinHttp.ResponseText

    Set objWinHttp = Nothing


end function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the filesystem extension