Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

Use VB to initiate a socket connection to PHP.

I have a VB program that I would like to talk to my webserver. The catch is, I want the VB program to initiate the connection. I am sure I have to use socket programming, but I don't know how to do it. Can you guys help?
Avatar of geir_andersen
geir_andersen
Flag of Norway image

I think your best bet is to post this question, or a pointer, in the VB section of Programming:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/

-Geir
Avatar of DrDamnit

ASKER

To be more clear: I am unclear on how to get PHP to listen, and also how to process requests that it recieves.
this function will enable you to send an http request by using msxml!!!

Function sendRequest$(url$, method$, content$)

'using XMLHttp in msxml3
Dim oHttp As XMLHTTP          'the client object
Dim strURL As String          'URL to be pulled
Dim mstrResponse As String
Dim aBytes() As Byte
Dim nbByte, i As Integer
Dim File      As String
Dim texte      As String

Dim timeStart As Date
Dim timeCurrent As Date
Dim timeOut As Boolean
Dim endLoop As Boolean

'create HTTP client
'On Error Resume Next
Set oHttp = CreateObject("msxml2.xmlhttp")         'for use on client
If oHttp Is Nothing Then
   MsgBox "Msxml 3.0 is not installed. You may download it from msdn.microsoft.com/xml", vbCritical
   Exit Function
End If

'get
'DoEvents
oHttp.open method$, url$, True
oHttp.setRequestHeader "Content-Type ", "application/octet-stream"
If method$ = "GET" Then
   oHttp.send
Else
   oHttp.send (content$)
End If

timeStart = Now
timeOut = False
endLoop = False
While endLoop = False
DoEvents
timeCurrent = Now - TimeSerial(0, 0, 30)
If timeCurrent > timeStart Then
   timeOut = True
   endLoop = True
Else
   If oHttp.readyState = 4 Then
      endLoop = True
   End If
End If
Wend

If timeOut = True Then
   sendRequest$ = False
Else
   mstrResponse = ""
   If Len(oHttp.responseBody) > 0 Then
      aBytes = oHttp.responseBody

      File = App.Path & "\" & App.Title & ".tmp"
      Open File For Binary As #1
      Put #1, , aBytes
      Close

      Open File For Binary As #1
      Put #1, , aBytes
      Close

      Open File For Input As #1
      Do While Not EOF(1)
         Line Input #1, texte
         mstrResponse = mstrResponse & texte & vbCrLf
      Loop
      Close #1

      Kill File
      'beautify output
      sendRequest$ = Replace(mstrResponse, vbLf, vbCrLf)
   Else
      sendRequest$ = False
   End If
 
End If

End Function
if you use the method that I have describe, you are not using socket programming on the VB side, you are simulating a browser connection, therefore your php program does not need to listne for socket or doing anything different that for a request coming from a browser!
Well, the whole point is so that I can have PHP query MySQL since my hosting provider doens't allow direct access to MySQL. Moreovre, I had a web interface to manage the data in the database, so I don't need to do anything more than that. I just need a reliable connection. Will the code above work even though it is not a socket connection?
ASKER CERTIFIED SOLUTION
Avatar of alain34
alain34
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial