# VBForums CodeBank > CodeBank - Visual Basic .NET >  Send POST Method to a PHP file through Visual Basic

## thefiscster

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



```
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:


```
 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.  :Big Grin: ";
?>


?>

----------


## FlashDark

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!

----------


## bensonsearch

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/

----------


## techgnome

> 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!





> 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

----------


## bensonsearch

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

----------


## FlashDark

> 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.

----------


## techgnome

> 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!




> 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

----------

