Results 1 to 13 of 13

Thread: Executing PHP Script from VB6 *RESOLVED*

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Executing PHP Script from VB6 *RESOLVED*

    Hello,
    I am trying to execute a PHP Script that sends an email populated with variable from a vb6 form. I am trying to use the post method, but not sure this is even the best way to accomplish my goal here is what I have:

    VB Code:
    1. Private Sub Command1_Click()
    2.     'text4 is just a url address
    3.     Winsock1.Connect Text4, 80
    4. End Sub
    5.  
    6. Private Sub Winsock1_Connect()
    7.     Dim strHttpRequest As String
    8.     Dim strPHP As String
    9.     strPHP = Text4 & "/vbMail.php"
    10.     strHttpRequest = "POST " & strPHP & " HTTP/1.0\r\n"
    11.     strHttpRequest = strHttpRequest _
    12.         & " Content-Length:65\r\n" & vbCrLf _
    13.         & " Content-Encoding: application/x-www-from-urlencoded\r\n\r\n" & vbCrLf _
    14.         & " Var1=Jesse&Var2=21&var3=Fort Collins" _
    15.         & vbCrLf
    16.     Winsock1.SendData strHttpRequest
    17. 'var1 var2 and var3 are the variables I am trying to send
    18. End Sub

    my PHP Script is:

    PHP Code:
    function ReturnValue($Field) {
            return 
    $Field;
        }
        
    $msg "Hello";
        
    $msg .= ReturnValue($_POST[Var1]);
        
    $msg .= ReturnValue($_POST[Var2]);
        
    $msg .= ReturnValue($_POST[Var3]);
        
    $recipient "[email protected]";
        
    $subject "VB6";
        
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        
    $headers .= "Reply-To: [email][email protected][/email]";
        
    mail($recipient,$subject,$msg,$headers);
    ?> 
    This does not work and I am new to PHP and very new to vb6 integrating with PHP. Thanks in advance for any help
    Jesse
    Last edited by wallacejww; Feb 21st, 2005 at 02:00 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6

    Which part of the application doesn't work and what errors, if any, are you getting?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Re: Executing PHP Script from VB6

    It Appears to go off with out a hitch It runs through all the connection code with no errors I just never get an email. I am pretty sure it is not the PHP portion however since if I access it though a browser window I do get an email.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6

    The problem is in your HTTP request. I recognise it from the reply I gave Electroman earlier. Let me explain it a little more.
    • \r\n is the PHP escape sequence for the carridge return and new line characters. Each header in the HTTP request must be seaparted by a CRLF line ending. As VB does not understand \r\n you need to use the vbCrLF constant. To signify the end of the headers and the start of the content we use a blank line.
    • The Content-Length header must be the exact number of characters in your message body. In this case you've used 65, which is incroeect.
    • The third problem, is you have told the server you will be using URL encoding in the message body but the data is not encoded properly. Namely the space hasn't been converted to %20 or a + chracter.

    This corrected version should work:
    VB Code:
    1. Private Sub Winsock1_Connect()
    2.     Dim strHttpRequest As String
    3.     Dim strPHP As String
    4.     Dim strData As String
    5.     Dim intContentLength As Integer
    6.  
    7.     strData = "Var1=Jesse&Var2=21&Var3=Fort+Collins"
    8.     intContentLength = Len(strData)
    9.    
    10.     strPHP = Text4 & "/vbMail.php"
    11.     strHttpRequest = "POST " & strPHP & " HTTP/1.0" & vbCrLf & _
    12.                      " Content-Length:" & intContentLength & vbCrLf  & _
    13.                      " Content-Type: application/x-www-from-urlencoded" & vbCrLf & _
    14.                      vbCrLf & strData
    15.    
    16.  Winsock1.SendData strHttpRequest
    17. End Sub
    Last edited by visualAd; Feb 21st, 2005 at 01:51 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Re: Executing PHP Script from VB6

    I got it to access the php script with this code

    VB Code:
    1. strData = "Var1=Jesse&Var2=21&Var3=Fort Collins"
    2.  
    3. CreateSendString = "POST /vbMail.php HTTP/1.1" & vbCrLf & _
    4.                     "Host: www.hostaddress.com" & vbCrLf & _
    5.                     "Content-Length: " & Len(strData) & vbCrLf & _
    6.                     vbCrLf & strData & _

    However it is not passing the variables listed in strData do you know the correct syntax for this.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6

    Did you read my post above?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Re: Executing PHP Script from VB6

    Yes and it also works to execute the php file but it still will not pass the 3 variables in strData.

    By the way thank you for all your help on this I really appreciate it.
    Last edited by wallacejww; Feb 17th, 2005 at 03:53 PM.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6

    What does the Tex4 variable in your code contain. You PHP script should be executed.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Re: Executing PHP Script from VB6

    Yes My php script is executed but the the variables are not passed. I am posting a copy of my form if you enter a valid email address you should get a email contaning Hello [first name] [last name].

    ps. You can go to http://www.phototix.com/temp.html to run the php script via a web browser.
    Attached Files Attached Files

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6

    My mistake, you should be sending the Content-Type header, not the Content-Encoding header. Also, make sure the HTTP request looks like this and uses the HTTP 1.0 otherwise the server may send back a chunked response, which needs to be re-assembled.
    VB Code:
    1. strHttpRequest = "POST " & Text4 & " HTTP/1.0" & vbCrLf & _
    2.                      "Host: www.phototix.com" & vbCrLf & _
    3.                      "Content-Length: " & Len(strData) & vbCrLf & _
    4.                      "Content-Type: application/x-www-form-urlencoded" & vbCrLf & _
    5.                      vbCrLf & strData
    P.s: don't send cache control headers as they are response headers and my apologies for giving you incorrect code
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Thumbs up Re: Executing PHP Script from VB6

    Thanks That Worked Great

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2003
    Posts
    41

    Wink Re: Executing PHP Script from VB6 *RESOLVED*

    I have since taken down the php script from my server. But for the benifit of this thread here is what it contained

    PHP Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <?php
    function ReturnValue($Field) {
            return 
    $Field;
        }
    ?>
    </head>

    <body>
    <?php
        $msg 
    "Hello ";
        
    $msg .= ReturnValue($_POST[Var1]);
        
    $msg .= ReturnValue($_POST[Var2]);
        
    $msg .= ReturnValue($_POST[Var3]);

        
    $recipient1 ReturnValue($_POST[Var3]);
        
    $subject "VB6 PHP Post";
        
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        
    $headers .= "Reply-To: ";
        
    $headers .= ReturnValue($_POST[Var3]);

        
    mail($recipient1,$subject,$msg,$headers);
    ?>
    </body>
    </html>
    Last edited by wallacejww; Mar 15th, 2005 at 01:36 PM.

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Executing PHP Script from VB6 *RESOLVED*

    What's the point of the ReturnValue function?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width