Announcement

Collapse
No announcement yet.

Server Exchange

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Howdy, Stuart!



    Well, hallelujah! At least from the 10 ft. level I had a correct vision - just not in the detailed implementation of the PostReq vs GetReq. I'll go back over the earlier posts looking for the GET option. I want to have an example of both in front of me. Thanks for being patient while I stumble through this.
    Posts 4 and 16

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Howdy, Stuart!

    Thanks for the response. I'm enjoying the discussion, even if I may not yet be in sync with what you're saying.

    Have to? Only if my PHP uses $_POST, yes?
    No, only if your application that is sending uses the HTTP POST method


    From MSDN I read this, which says GET is a legit verb for Open method.

    ...for the Open method, such as "GET" or "PUT".

    So can't I use $_GET in the PHP and pHttpReq.Open "GET" along with a PBWin function "GetReq"?
    Yes you can, but read my W3Schools link and you should see why I am pushing you to use POST rather than GET for this sort of operation.

    You can use the one POST function for just about anything. GET is much more restricted. It has a number of limitations as well as various security implcations.

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Stuart!

    Yes there's a parallel GET function
    Well, hallelujah! At least from the 10 ft. level I had a correct vision - just not in the detailed implementation of the PostReq vs GetReq. I'll go back over the earlier posts looking for the GET option. I want to have an example of both in front of me. Thanks for being patient while I stumble through this.

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Stuart!

    Thanks for the response. I'm enjoying the discussion, even if I may not yet be in sync with what you're saying.


    you have to specify POST, not GET.
    Have to? Only if my PHP uses $_POST, yes?


    From MSDN I read this, which says GET is a legit verb for Open method.

    [QUOTE]...for the Open method, such as "GET" or "PUT".

    So can't I use $_GET in the PHP and pHttpReq.Open "GET" along with a PBWin function "GetReq"?


    Are you saying there are NOT two solutions, one for GET and one for POST (a pair of PHP/PBWin for each approach)?

    Added: You're faster than I.

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Sorry, but I'm slow today.

    The calling of the PHP for a GET is like this, with the data included in the URL and the PHP using $_GET.

    Code:
    http://newvisionconcepts.com/subscriptions.php?param1=testvalue
    But for a POST, the data cannot be included in the URL so there is no URL like the one above to run a PHP with $_POST. But the POST request can be sent using the PostREQ code as Stuart posted. In that case, the PHP must also use $_POST.

    Is there a parallet GetReq function? Is it simply using Get in place of Post in Stuart's function? And in that case, I'd assume the PHP must also use $_GET. The point of GetReq function is receive the PHP output via code, just as Stuart's PostReq does it.
    Yes there's a parallel GET function. Some earlier code in this thread has a function where you specified GET or POST as a function parameter.

    It's not simply a matter of replacing POST with GET in the function.
    For GET, you have to append the parameters to the URL (including a leading '&") and there are no parameters to the Send statement
    For POST,. you put the parameters in the Send statement (with no leading "&")


    Despite the safety aspect that Stuart points out, one draw to GET is that I can create a URL on the fly and test it in the browser, no PBWin code needed.

    I'll play with it some more until I think I understand it better!
    Wanna test a Post?
    Click image for larger version  Name:	PostTest.jpg Views:	0 Size:	47.3 KB ID:	819158



    '
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "httprequest.inc"
    ENUM ctrls SINGULAR
        IDC_btnAction= 1001
        IDC_URL
        IDC_Params
        IDC_lbl1
        IDC_lbl2
    END ENUM
    
    FUNCTION PBMAIN() AS LONG
        LOCAL lRslt AS LONG
        DIALOG DEFAULT FONT "consolas",12
        LOCAL hDlg  AS DWORD
    
        DIALOG NEW 0, EXE.FULL$, , , 350, 80, %WS_SYSMENU, TO hDlg
        CONTROL ADD LABEL , hDlg,%IDC_lbl1,"URL",10,10,40,14
        CONTROL ADD LABEL , hDlg,%IDC_lbl2,"Params",10,25,40,14
        CONTROL ADD TEXTBOX , hDlg,%IDC_URL,"",50,10,280,14
        CONTROL ADD TEXTBOX , hDlg,%IDC_Params,"",50,25,280,14
        CONTROL ADD BUTTON , hDlg,%IDC_btnAction,"Test POST",60,40,80,20
        DIALOG SHOW MODAL hDlg, CALL MainDlgCB TO lRslt
    END FUNCTION
    
    CALLBACK FUNCTION MainDlgCB()
        LOCAL wsUrl,wsRequest,wsReturn AS WSTRING
        LOCAL lResult AS LONG
        SELECT CASE AS LONG CB.MSG
            CASE %WM_COMMAND
                SELECT CASE AS LONG CB.CTL
                    CASE %IDC_btnAction
                        IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                            CONTROL GET TEXT CB.HNDL,%IDC_URL TO wsURL
                            CONTROL GET TEXT CB.HNDL,%IDC_Params TO wsRequest
                            lResult = lResult = PostReq(wsUrl,wsRequest,wsReturn)
                            IF lResult > 0 THEN
                               ? STR$(lResult) & ": " & wsReturn,%MB_ICONERROR,"POST Error"
                            ELSE
                                ? wsReturn,,"POST OK"
                            END IF
                        END IF
                END SELECT
        END SELECT
    END FUNCTION
    
    FUNCTION PostReq(wsURL AS WSTRING, wsRequest AS WSTRING,wsReturn AS WSTRING) AS LONG
        DIM pHttpReq AS IWinHttpRequest
        pHttpReq = NEWCOM "WinHttp.WinHttpRequest.5.1"
        IF ISNOTHING(pHttpReq) THEN wsReturn = "WinHttpRequest.5.1 failure" :FUNCTION = 1:EXIT FUNCTION
        TRY
            pHttpReq.Open "POST", wsURL, 0
            pHttpReq.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
            pHttpReq.Send(wsRequest)
            IF pHttpReq.StatusText <> "OK" THEN
                IF pHttpReq.StatusText = "" THEN
                    wsReturn =  "No response From Server" : FUNCTION = 2:EXIT FUNCTION
                ELSE
                    wsReturn = "Server returned: " & pHttpReq.StatusText:FUNCTION = 3:EXIT FUNCTION
                END IF
            ELSE  'success
                wsReturn = pHttpReq.Responsetext
            END IF
        CATCH
            wsReturn =  "COM Error"& $LF & HEX$(OBJRESULT,8) & ": " & OBJRESULT$()
            FUNCTION = 4
        END TRY
     END FUNCTION
    '

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Howdy, Stuart!

    I'm having trouble trying to follow your suggestion ...
    ...
    But if all I do is replace GET with POST, nothing is returned.

    Code:
    $serialnumber = $_POST["param1"];
    echo $serialnumber;
    exit;
    Is there more too it than just replacing GET with POST?

    Added: Am I correct in assuming that your PostReq function will not work with a $_GET PHP as well?
    You are still missing the difference between GET and POST in HTTP.

    I refer you again to https://www.w3schools.com/tags/ref_httpmethods.asp
    Also look at https://www.w3schools.com/php/php_forms.asp
    This is essentail basic knowledge if you are going to be doing anything more than creating static HTML web pages.

    In your application, you have to specify POST, not GET.


    Use this function:
    '
    Code:
    FUNCTION PostReq(wsURL AS WSTRING, wsRequest AS WSTRING,wsReturn AS WSTRING) AS LONG
        DIM pHttpReq AS IWinHttpRequest
        pHttpReq = NEWCOM "WinHttp.WinHttpRequest.5.1"
        IF ISNOTHING(pHttpReq) THEN wsReturn = "WinHttpRequest.5.1 failure" :FUNCTION = 1:EXIT FUNCTION
        TRY
            pHttpReq.Open "POST", wsURL, 0
            pHttpReq.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
            pHttpReq.Send(wsRequest)
            IF pHttpReq.StatusText <> "OK" THEN
                IF pHttpReq.StatusText = "" THEN
                    wsReturn =  "No response From Server" : FUNCTION = 2:EXIT FUNCTION
                ELSE
                    wsReturn = "Server returned: " & pHttpReq.StatusText:FUNCTION = 3:EXIT FUNCTION
                END IF
            ELSE  'success
                wsReturn = pHttpReq.Responsetext
            END IF
        CATCH
            wsReturn =  "COM Error"& $LF & HEX$(OBJRESULT,8) & ": " & OBJRESULT$()
            FUNCTION = 4
        END TRY
     END FUNCTION
     '
    ​and call it from something like this:
    '
    Code:
    FUNCTION PBMAIN()
        LOCAL wsurl,wsRequest,wsReturn AS WSTRING
        LOCAL lResult AS LONG
        wsURL = "http://newvisionconcepts.com/subscriptions.php"
        wsRequest = "param1=testvalue"
        lResult = PostReq(wsUrl,wsRequest,wsReturn)
        IF lResult > 0 THEN
           ? STR$(lResult) & ": " & wsReturn,%MB_ICONERROR,"POST Error"
        ELSE
            ? wsReturn,,"POST OK"
        END IF
    END FUNCTION
    '

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Howdy, Stuart!

    Thanks for the comments!

    Yes, I first tried str_contains first but the script would fail. From what I've read, that function requires PHP 8. I don't know what version my server has available but I never could get str_contains to work.
    .
    Ah yes, it was only introduced in PHP8 ( I didn't think of that) . From Post # 15, you are still on PHP 5.6.40.

    PHP version is usually an option you can set on your server for individual domains, up to the latest version your ISP has installed.
    i.e. If you have CPanel access or similar, you should see something like this
    :Click image for larger version  Name:	PHP1.jpg Views:	0 Size:	13.7 KB ID:	819154
    Click image for larger version  Name:	PHP2.jpg Views:	0 Size:	27.8 KB ID:	819155

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Dale!

    I should have been more clear. You're correct that there is a URL as is used in Stuart's PostReq function. But there is no URL that you just manually type into the browser to do run a POST PHP. Is that correct?

    Leave a comment:


  • Dale Yarker
    replied
    AFAIC there is still a URL, but you're not appending data it (as with GET).
    POST is the content going to that URL. Stuart has done it once or twice in this thread.

    Cheers,

    Leave a comment:


  • Gary Beene
    replied
    Sorry, but I'm slow today.

    The calling of the PHP for a GET is like this, with the data included in the URL and the PHP using $_GET.

    Code:
    http://newvisionconcepts.com/subscriptions.php?param1=testvalue
    But for a POST, the data cannot be included in the URL so there is no URL like the one above to run a PHP with $_POST. But the POST request can be sent using the PostREQ code as Stuart posted. In that case, the PHP must also use $_POST.

    Is there a parallet GetReq function? Is it simply using Get in place of Post in Stuart's function? And in that case, I'd assume the PHP must also use $_GET. The point of GetReq function is receive the PHP output via code, just as Stuart's PostReq does it.

    Despite the safety aspect that Stuart points out, one draw to GET is that I can create a URL on the fly and test it in the browser, no PBWin code needed.

    I'll play with it some more until I think I understand it better!

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Stuart!

    I'm having trouble trying to follow your suggestion ...

    If you use POST rather than GET
    With GET, this code works fine - returns the param1 value as in this URL:
    http://newvisionconcepts.com/subscri...ram1=testvalue

    Code:
    $serialnumber = $_GET["param1"];
    echo $serialnumber;
    exit;
    ​​​

    But if all I do is replace GET with POST, nothing is returned.

    Code:
    $serialnumber = $_POST["param1"];
    echo $serialnumber;
    exit;
    Is there more too it than just replacing GET with POST?

    Added: Am I correct in assuming that your PostReq function will not work with a $_GET PHP as well?

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Stuart!

    Thanks for the comments!

    Yes, I first tried str_contains first but the script would fail. From what I've read, that function requires PHP 8. I don't know what version my server has available but I never could get str_contains to work.

    That's a good point about simplifying the returned value by leaving of the HTML content of the PHP file. It makes sense now that you say it but I never saw that detail mentioned in my limited reading. I just assumed that the script returned only the echo stuff - not the HTML code surrounding the PHP script. A bad assumption on my part.

    It's particularly useful because using that approach i won't have to do any parsing of the returned content (unless I choose to do so) because the results will be just the echo arguments! Very cool! Thanks for pointing that out.

    Leave a comment:


  • Stuart McLachlan
    replied
    BTW: Consider str_contains​ rather than strpos

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Gary Beene View Post
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $serialnumber = $_GET["param1"];
    
    //read subscriptions.txt into a variable $content
    $filename = "subscriptions.txt";
    $fh = fopen($filename,"r");
    $content = fread($fh,filesize($filename));
    fclose($fh);
    
    //test for serial number in $content
    $result = "InActive";
    If (strpos($content,$serialnumber) !== false) {$result = "Active";}
    
    echo $result;
    
    ?>
    
    </body>
    </html>​
    A few points:
    You should always check whether there is a parameter supplied before allocating its value. If there is no "param1", PHP will throw an error.
    If you use POST rather than GET, your user can't snoop on your application with something like WIreshark and see what you are sending
    if you want to get a simple response, You should delete everything outside of the <?php..... ?> (You will not be trying to view it in an HTML viewer such as a browser!)
    Your page returns:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    InActive
    </body>
    </html>​​
    ​
    rather than just
    Code:
    InActive
    Try this (preferably substituting POST for GET)
    Code:
    <?php
    if(isset($_GET["param1"])) {
        $serialnumber = $_GET["param1"];
        //read subscriptions.txt into a variable $content
        $filename = "subscriptions.txt";
        $fh = fopen($filename,"r");
        $content = fread($fh,filesize($filename));
        fclose($fh);
        //test for serial number in $content
        $result = "InActive";
        If (strpos($content,$serialnumber) !== false) {$result = "Active";}
        echo $result;
    }
    ?>



    Leave a comment:


  • Gary Beene
    replied
    Howdy, Stuart!

    Along the lines of your PHP in post 49 ...

    With a "subscriptions.txt" file containing lines than contain a keyword and other text, in any order, here is a PHP that simply determines if a keyword is found anywhere in the file.

    For this to work, the keyword has to be unique, such as a drive serial number, so that nothing else on the lines can be mistaken for the keyword.

    Sample content
    Code:
    0000_0000_0100_0000_E4D2_5C00_6CDF_5101 Mary Jones 
    0000_0000_0200_0000_E4D2_5C00_6CDF_5101 Tom Jones
    0000_0000_0300_0000_E4D2_5C00_6CDF_5101 Lucas Walker
    0000_0000_0400_0000_E4D2_5C00_6CDF_5101 Marvin Runner
    0000_0000_0500_0000_E4D2_5C00_6CDF_5101 Gary Sitting
    0000_0000_0600_0000_E4D2_5C00_6CDF_5101 Stuart Action
    0000_0000_0700_0000_E4D2_5C00_6CDF_5101 Mike Working​
    Then the very simple PHP ..

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $serialnumber = $_GET["param1"];
    
    //read subscriptions.txt into a variable $content
    $filename = "subscriptions.txt";
    $fh = fopen($filename,"r");
    $content = fread($fh,filesize($filename));
    fclose($fh);
    
    //test for serial number in $content
    $result = "InActive";                              
    If (strpos($content,$serialnumber) !== false) {$result = "Active";}
    
    echo $result;
    
    ?>
    
    </body>
    </html>​
    This fits the scenario where a server list is kept of the drive serial numbers whose owner has purchased a subscription. The PBWin app could use wmic code to get the user's serial number and then use your PostREQ code to access the PHP to validate the subscription.




    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Dale Yarker View Post
    Gary,

    You are trying to use as few lines as possible; but HTML is wordy, just get used to it.
    For his original requirement, he doesn't need any HTML (Hypertext Markup Language), all he needs is a HTTP (Hypertext Transfer Protocol) server and some sort of scripting interpreter such as PHP/Perl,Python etc on it and a routine to POST plain text to it.

    "From my PC PBWin app, I want to send a string to a server app, have the server app look up the string in a text database, then return the string and any associated information to my PBWin app."

    We've got distracted by GETing HTML web pages (gbsnippets0052.htm etc) , but that is really beside the point.
    What it boils down to is the following - no HTML anywhere (unless you want to program your script to return HTML formatted text):
    '
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "httprequest.inc"   'José Roca includes
    
    FUNCTION PBMAIN()
        LOCAL wsurl,wsRequest,wsReturn AS WSTRING
        LOCAL lResult AS LONG
        wsURL = "" ' Insert the URL to your PHP or whatever script here
        wsRequest = "" 'Insert whatever data you want to send to the script on the server here
        lResult = PostReq(wsUrl,wsRequest,wsReturn) ' This is either the response or an error message
        IF lResult > 0 THEN
           ? wsReturn,%MB_ICONERROR,"POST Error"
        ELSE
            ? wsReturn,,"POST OK"
        END IF
    END FUNCTION
    
    FUNCTION PostReq(wsURL AS WSTRING, wsRequest AS WSTRING,wsReturn AS WSTRING) AS LONG
        DIM pHttpReq AS IWinHttpRequest
        pHttpReq = NEWCOM "WinHttp.WinHttpRequest.5.1"
        IF ISNOTHING(pHttpReq) THEN wsReturn = "WinHttpRequest.5.1 failure" :FUNCTION = 1:EXIT FUNCTION
        TRY
            pHttpReq.Open "POST", wsURL, 0
            pHttpReq.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
            pHttpReq.Send(wsRequest)
            IF pHttpReq.StatusText <> "OK" THEN
                IF pHttpReq.StatusText = "" THEN
                    wsReturn =  "No response From Server" : FUNCTION = 2:EXIT FUNCTION
                ELSE
                    wsReturn = pHttpReq.StatusText:FUNCTION = 3:EXIT FUNCTION
                END IF
            ELSE  'success
                wsReturn = pHttpReq.Responsetext
            END IF
        CATCH
            wsReturn =  "COM Error"& $LF & HEX$(OBJRESULT,8) & ": " & OBJRESULT$()
            FUNCTION = 6
        END TRY
     END FUNCTION
     '

    Leave a comment:


  • Dale Yarker
    replied
    Gary,

    You are trying to use as few lines as possible; but HTML is wordy, just get used to it.

    Cheers,

    Leave a comment:


  • Dale Yarker
    replied
    But Gary's multiple CHR$(160)s aren't ASCII
    Right! Just invalid.

    declare CHARSET=8859-1 for CHR$(160)
    or
    CHARSET=UTF-8 for CHR$(&hA0, &hC2) 'if poking a word C2A0
    (the A0 is a coincidence because in UTF-8 bits 7 and 6 of byte 0 mark as multibyte character.)
    or
    CHARSET=UTF-16 for CHR$$(&h00A0)

    Forcing a character from one CHARSET into an assumed other, or declared as other CHARSET is invalid from git-go. PICK ONE!

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Dale Yarker View Post
    But hasn't "stuck" yet, so again won't hurt.
    Note non wide string as parameter of UTF8TOCHR$. And all HTML statements/tags are ASCII; identical to UTF-8 (and other CHARSETs) for characters below 128.
    But Gary's multipe CHR$(160)s aren't ASCII - hence the issue with trying to decode them correctly in an assumed UTF8 string. They would need to be either the UTF8 two byte "&HC1A0" or the HTML entity "&nbsp;"



    Leave a comment:


  • Rod Macia
    replied
    Yes we know that Gary's page is malformed, an server gives wrong character set info. and there is nothing we can do about it other than guess, Save as is, or spend a lot of time analyzing.

    But Dale's point is valid that any UTF8TOCHR$(buffer) conversion from web content should go into a WSTRING to preserve as many as the Unicode characters, UTF-8 encoded. For well formed, properly identifying HTML pages.

    Unless all you need is to save as is, which should not require any conversion.

    Leave a comment:

Working...
X