HTTP request

Costas

Administrator
Staff member
JavaScript:
Function AskService()
'XMLHTTP open fuction - https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms763809(v=vs.85)
' FAQ - http://excelerator.solutions/2017/08/28/excel-http-get-request/
' FAQ2 - http://excelerator.solutions/2017/08/16/import-json-to-excel-using-vba/
' Cell Get/Set - https://www.automateexcel.com/vba/cell-value-get-set/

'https://stackoverflow.com/a/52903455
'  With CreateObject("MSXML2.ServerXMLHTTP.6.0")
'    .Open "GET", "http://localhost/", False
'    .Send
'    getHTTP = StrConv(.responseBody, vbUnicode)
'  End With

Dim hReq As Object
Dim JSON As Object
Dim i As Long

Set hReq = New XMLHTTP60 '(req "Microsoft XML, v6.0")

With hReq
       .Open "POST", "http://localhost/", False
       .setRequestHeader "If-Modified-Since", Time() 'IGNORE CACHE - https://stackoverflow.com/a/8039006
       .Send
End With

Dim response As String
response = hReq.ResponseText
    
Set JSON = JsonConverter.ParseJson(response) 'https://github.com/VBA-tools/VBA-JSON (req "Microsoft Scripting Runtime") -- used also by https://github.com/Bearx2004/vb6jsonx

i = 1
For Each o In JSON
    ThisWorkbook.Sheets("mydata1").Range("A" & i).Value = o("company") 'change to current workbook and sheet name mydata1
    'Range("A" & i).Value = o("company") when apply changes go to active excel, possible other file..
    i = i + 1
   ' Debug.Print (o("company"))
Next

End Function

PHP Server side
JavaScript:
<?php

require_once('general.php'); //https://github.com/pipiscrew/DBManager/blob/master/1_updatePHP/pagination/general.php

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        die("bad request");
        exit;
} 
    
$db = new dbase();

$db->connect_mysql();

//MS.northwind dbase
$r = $db->getSet("select * from customers", null);

echo json_encode($r);

for test purposes added some unicode chars at dbase (excel vs dbase)
AuVzUUg.png


EjV1EQe.png
 
Top