HTTP Get Request Using VBA in OSX Excel

前端 未结 2 1584
灰色年华
灰色年华 2021-02-10 09:16

I\'m writing a very simple macro that needs to make an HTTP GET request to a server and the response is not important (it initiates a process on the server). The HTTP GET does N

2条回答
  •  春和景丽
    2021-02-10 10:03

    This wasn't working for me on Mac Excel 15.4 either. I found a helpful version on VBA-Tools Github Here:

    https://github.com/VBA-tools/VBA-Web/issues/248

    Here's the version that worked for me:

    Private Declare PtrSafe Function web_popen Lib "libc.dylib" Alias "popen" (ByVal command As String, ByVal mode As String) As LongPtr
    Private Declare PtrSafe Function web_pclose Lib "libc.dylib" Alias "pclose" (ByVal file As LongPtr) As Long
    Private Declare PtrSafe Function web_fread Lib "libc.dylib" Alias "fread" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
    Private Declare PtrSafe Function web_feof Lib "libc.dylib" Alias "feof" (ByVal file As LongPtr) As LongPtr
    
    Public Function executeInShell(web_Command As String) As String
    
        Dim web_File As LongPtr
        Dim web_Chunk As String
        Dim web_Read As Long
    
        On Error GoTo web_Cleanup
    
        web_File = web_popen(web_Command, "r")
    
        If web_File = 0 Then
            Exit Function
        End If
    
        Do While web_feof(web_File) = 0
            web_Chunk = VBA.Space$(50)
            web_Read = web_fread(web_Chunk, 1, Len(web_Chunk) - 1, web_File)
            If web_Read > 0 Then
                web_Chunk = VBA.Left$(web_Chunk, web_Read)
                executeInShell = executeInShell & web_Chunk
            End If
        Loop
    
    web_Cleanup:
    
        web_pclose (web_File)
    
    End Function
    
    
    Function getHTTP(sUrl As String, sQuery As String) As String
        Dim sCmd As String
        Dim sResult As String
        Dim lExitCode As Long
        sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
        sResult = executeInShell(sCmd)
        getHTTP = sResult
    End Function
    

提交回复
热议问题