Send POST Method to a PHP file through Visual Basic-VBForums
Results 1 to 7 of 7

Thread: Send POST Method to a PHP file through Visual Basic

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Location
    United States
    Posts
    26

    Resolved Send POST Method to a PHP file through Visual Basic

    This is a function I did that will allow you to send data to a PHP file via the POST method...

    Code:
    Public Function PHP(ByVal url As String, ByVal method As String, ByVal data As String)
            Try
    
                Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
                request.Method = method
                Dim postData = data
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
                request.ContentType = "application/x-www-form-urlencoded"
                request.ContentLength = byteArray.Length
                Dim dataStream As Stream = request.GetRequestStream()
                dataStream.Write(byteArray, 0, byteArray.Length)
                dataStream.Close()
                Dim response As WebResponse = request.GetResponse()
                dataStream = response.GetResponseStream()
                Dim reader As New StreamReader(dataStream)
                Dim responseFromServer As String = reader.ReadToEnd()
                reader.Close()
                dataStream.Close()
                response.Close()
                Return (responseFromServer)
            Catch ex As Exception
                Dim error1 As String = ErrorToString()
                If error1 = "Invalid URI: The format of the URI could not be determined." Then
                    MsgBox("ERROR! Must have HTTP:// before the URL.")
                Else
                    MsgBox(error1)
                End If
                Return ("ERROR")
            End Try
        End Function
    Uses:
    Code:
     Dim htmlcode As String = PHP("http://somesite.com/somephpfile.php", "POST", "name=Jim&age=27&pizza=suasage")
    Then you could have a PHP file that has something like

    <?PHP
    echo $_POST["name"] . " is " $_POST["age"] . " and he likes " . $_POST["pizza"] . " pizza. ";
    ?>


    ?>
    If I fail... You can prolly go anywhere else and get teh right answer

    Please Visit my site:
    http://www.fiscalleti.com/

  2. #2
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Re: Send POST Method to a PHP file through Visual Basic

    This is very useful. I was working a lot on MySQL database, which was on localhost, and then got shocked after figuring out that there is no way to connect to remote MySQL server. This solution saved my day (or week), even though there will be some extra work. Thanks a lot!

  3. #3
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    598

    Re: Send POST Method to a PHP file through Visual Basic

    Hi,
    awesome post thanx,
    FlashDark - there is most definately ways to connect to MySQL, the main one, you need to get the MySQL connector net: http://dev.mysql.com/downloads/connector/net/
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here

  4. #4
    PowerPoster
    Join Date
    May 2002
    Posts
    25,261

    Re: Send POST Method to a PHP file through Visual Basic

    Quote Originally Posted by FlashDark View Post
    This is very useful. I was working a lot on MySQL database, which was on localhost, and then got shocked after figuring out that there is no way to connect to remote MySQL server. This solution saved my day (or week), even though there will be some extra work. Thanks a lot!
    Quote Originally Posted by bensonsearch View Post
    Hi,
    awesome post thanx,
    FlashDark - there is most definately ways to connect to MySQL, the main one, you need to get the MySQL connector net: http://dev.mysql.com/downloads/connector/net/
    It depends on where that remote database is located... I have a couple MySQL databases that are hosted, but I cannot access them remotely... I can only access them from within my host's domain... specifically either through the cpanel or from my webpages... so if I want to access the data outside of the firewall, I'd have to develop a web service of some kind that would then allow me access.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    598

    Re: Send POST Method to a PHP file through Visual Basic

    Hi techgnome,
    thats interesting, I found that issue but used the hosted cpanel to allow remote connections to the server from specific IP address's. I guess it all comes down to setup
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here

  6. #6
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Re: Send POST Method to a PHP file through Visual Basic

    Quote Originally Posted by bensonsearch View Post
    Hi techgnome,
    thats interesting, I found that issue but used the hosted cpanel to allow remote connections to the server from specific IP address's. I guess it all comes down to setup
    As techgnome said, some shared hosting providers just won't allow you to connect to MySQL remotely because of security reasons, as it's in my particular situation.

  7. #7
    PowerPoster
    Join Date
    May 2002
    Posts
    25,261

    Re: Send POST Method to a PHP file through Visual Basic

    Quote Originally Posted by bensonsearch View Post
    Hi techgnome,
    thats interesting, I found that issue but used the hosted cpanel to allow remote connections to the server from specific IP address's. I guess it all comes down to setup
    rule of thumb - when dealing with remote/hosted databases... always check the host first to see if it's possible and/or if something need to be configured. While some do offer a whitelist... it can sometimes be more of a hassle... if you're the only one accessing it, then it's not much of a problem (until your ISP decides to give you a new IP) ... but if you're planning on distributing the app, where multiple people will have access... then you have to add all of their IPs to the white list... and then if they ever change....oi!

    Quote Originally Posted by FlashDark View Post
    As techgnome said, some shared hosting providers just won't allow you to connect to MySQL remotely because of security reasons, as it's in my particular situation.
    Yeah, that's the case for me too... it's amazing though, there are a number that are wide open... no white list, no black list, not firewall of any kind.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.