No announcement yet.

Invoke PHP Script

  • Filter
  • Time
  • Show
Clear All
new posts

  • Invoke PHP Script

    I want to send a string from a PowerBASIC app that invokes a PHP script that is on my server. I'd then like to receive the output produced by that PHP script.

    I could do this from within an embedded browser, but without a browser I'm not sure how to even start.

    Can anyone give me some pointers?

  • #2
    I did find a post here that Dominic Sexton posted, which he uses to post data to a php script and, according to Dominic returns the body of the web page generated by the post method. Looking at it ...


    • #3
      Thinking about my OP, I may have constrained the solution to more than I intend to do.

      Here's a different description of what I'm trying to do.

      The PowerBASIC app would allow my software users to send some comments to the server PHP script, which would simply add the comments to a server text file, then tell the PowerBASIC app that the new text file is available. At that point the PowerBASIC app would simply download and display the content of the text file to the user.

      It's just a way to let my users post comments, that all my users could see, without going through a browser.

      I could, for example, add it to all of my gbXXX apps, making it easier for users to provide feedback.


      • #4
        It also would let users of my software have essentially a "one-topic forum" where they could post questions to me or to talk amongst themselves - with the message traffic available to all users. It's hardly a "forum", but might be way easier for me to implement and maintain than the multi-purpose forums or blog response software that's out there.


        • #5
          Getting close, I think. Here are some PHP and PowerBASIC test code, where I'm just trying to echo back the content sent to the PHP script by the PowerBASIC code.

          At the moment, the sent string which I expect to be in $_POST, doesn't get echoed out using the foreach loop, or doesn't get sent correctly by the PowerBASIC app - I'm not sure which.

          Once I have the $_POST content, I can write that to a file, so in this minimal example I'm just trying to confirm that I can access $_POST content (the string sent by the PowerBASIC app).

          I've got 4th of July celebrations to go to, but I'll be back late tonight to see if I can finish it up.

          This PHP, which returns the date/time and the string sent by the PowerBASIC app ...
          <!DOCTYPE html><html><body>
          echo date("d-m-Y  h-m-s");
          foreach ($_POST as $key => $value) {
            echo $value;
          plus this PowerBASIC code, drawn from Dominic's post ...

          'Compilable Example:
          #Compile Exe
          #Dim All
          %Unicode = 1
          #Include ""
          Function PBMain() As Long
             Local sHead$, sResponse$
             sResponse$ = WebPost("", "/chat/chat.php", "Send this string", sHead$)
             ? "Header:----------" + $CrLf + sHead$ + $CrLf + $CrLf + "PHP output:----------" + $CrLf + sResponse$
          End Function
          Function WebPost(sServer$, sPHPCom$, sData$, sHead$) As String
             Local sTemp$, recBuffer$   'returns body of generated web page
             Tcp Open Port 80 At sServer$ As #1 TimeOut 1000  'Connect to the server
             Tcp Print #1, "POST " + sPHPCom$ + " HTTP/1.0"
             Tcp Print #1, "Accept: */*"
             Tcp Print #1, "Accept-Language: en-us"
             Tcp Print #1, "Host: " + sServer$
             Tcp Print #1, "User-Agent: web-poster"
             Tcp Print #1, "Content-Type: application/x-www-form-urlencoded"
             Tcp Print #1, "Content-Length: " + Format$(Len(sData$))
             Tcp Print #1, ""
             Tcp Print #1, sData$
             Sleep 80
              Tcp Recv #1, 1024, recBuffer$
              stemp$ = stemp$ & recBuffer$
             Loop Until (IsFalse Len(recBuffer$) And IsTrue Eof(1)) Or IsTrue Err
             Tcp Close #1
             sHead$ = Extract$(sTemp$, $CrLf + $CrLf)
             WebPost = LTrim$(Remain$(sTemp$, $CrLf + $CrLf), $CrLf)
          End Function


          • #6
            Without reading through your post throughly.
            I am assuming your server has embedded php scripting.
            The phpinfo php command will display a lot of info about your server from a .php file accessed by a browser.
            You should be able to access a php file on your site directly from Powerbasic.
            If you want to use webpage redirection from within a HTML file to the php file. You will might have to use windows API http functions.
            You may also choose to name your file index.php in a website directory removing any other "index" named files
            p purvis


            • #7
              Your code isn't sending any parameters to echo back, that's why nothing (other than the date and time) is returned.

              Try POSTing real parameters and they will be echoed back.
              "Send this string" to "text=And now is the time for all good men to come to the aid of the party" and try it again.


              • #8
                Hi Paul!
                Thanks for responding.

                Yes, the code above does access/invoke a PHP script just fine, it seems - barring me not understanding why the passed string is not retrieved using the foreach loop over the associative array $_POST in the PHP script..

                To get the updated file, I can either choose to read the updated file and have the PHP script pass its content to my PowerBASIC app OR I can use the URLDownloadToFile to retrieve it.

                I'm not sure I understand why redirection would be necessary/desirable. Perhaps you can explain that option a bit more?

                Naming the file index.php will only be useful in removing a few characters from the URL path, yes? Or was there another benefit that I should know about benefit you're aware of that I should know about?


                • #9
                  Howdy, Paull!

                  "Send this string" to "text=And now is the time for all good men to come to the aid of the party" and try it again.
                  Yes, I did try that and it did not work. However, I was dong some tests very quickly because I needed to head out to dinner.

                  I'll try it again to see if haste makes waste .


                  • #10
                    Well, Paul,
                    A quick redo per your suggestion and the string did come through! The haste makes waste theory seems proven once again!

                    However, I did hope that with no named element, that the entire string/content would be put in the [0] element of the array $_POST. Apparently that safety net does not happen. Or, perhaps it does and I just don't know the right catch-all element name?

                    The way that PARSE$(sData$,$spc) would return 1 even with no delimiters perhaps swayed my thinking on how PHP would act.


                    • #11
                      Ok, off to watch fireworks. Thanks for the help, guys!


                      • #12
                        I've used the code that was discussed above to create a lightweight messenger/chat/forum application, gbTweet. You can see it in the source code forum, here.

                        Multiple users can chat using gbTweet, but it only supports a single "thread". All user tweets are presented to all other users as a simple listing of tweets. The user name and date are sent with the tweet, as seen in this gbTweet image:

                        Click image for larger version  Name:	gbtweet.jpg Views:	2 Size:	32.4 KB ID:	773493

                        Users can send a single line tweet of up to 250 characters. Once a tweet is submitted, gbTweet adds it to the online tweet history file, then downloads the current history file for display in the local app.

                        Users can update the tweet history manually, or have gbTweet update the history automatically on intervals of their choosing (5s, 10s, 30s, 60s).

                        gbTweet does not support private conversations, images, nor active links. And, there is NO real security associated with gbTweet at this time. You should consider all tweets as available to any interested users.

                        Currently, the PHP file and tweet history is located on my own server. If you wanted a messenger for your own group of family, friends or associates you'd simply put the PHP file on your own server and edit the local app INI files values that point to that server, or change the app code itself to include your server information.

                        gbTweet requires no password and no registration is required.

                        Feature Summary:
                        • Single thread Chat/Forum/Text Messenger
                        • Multiple users
                        • Automatic tweet history updates
                        • User Name and Date added to user comments
                        • Tweet History archived locally
                        • Remote clearing of tweet history
                        Give it a try and let me know how it works. For now, I'll leave my server copy running for anyone to use.


                        • #13
                          For several months now, I've considered writing this app, but never gave it that much attention because I thought it would take too much effort. But as you can see in the source code (or in post #5 above), the core code is incredible minimal - barely 50 lines of PowerBASIC code. It could obviously use more debugging code and better security features, but still, it was surprisingly easier to do than I expected. My thanks for Dominic Sexton for his TCP code, which I modified for part of this app.

                          I picked up the PHP code primarily from reading tutorials at There's nothing in the PHP script but some basic functions - something that I assume also could be done with a PBCC server-side app as well.


                          • #14
                            Oh ... if you include the string "fffjjj" in your tweet, it will clear the server tweet history, leaving just a Welcome comment. I've posted no tweets of lasting value, so feel free to test that out yourself.

                            And, of course, don't be surprised if anything you tweet disappears when someone is playing with it!

                            When I was a boy living in the country, we had a "party line" telephone, meaning only one call could be made at a time. And, anyone could pick up the phone and listen in to an ongoing conversation. I recall that there were lots of of nosey neighbors in that situation.


                            • #15
                              I just made a tweet with some punctuation marks in it,which came back with some special character formatting from the PHP script. I'll watch for that and add conversions to the PowerBASIC app as I figure out what is needed. I'm sure the HTML folks here will recognize what is happening and will know exactly what to do.


                              • #16
                                ,which came back with some special character formatting from the PHP script.
                                Maybe you need this?:


                                • #17
                                  Hi Paul,

                                  Thanks for pointing out the additional PHP functions. In this case, the PHP function html_entity_decode seems to be the right fit. I'll go give it a try ...


                                  • #18
                                    No, that wasn't it either. There' is a PHP function "stripslashes" but that doesn't seem to work (surely, my fault).

                                    For the moment I've just added a backslash removal line of code in the PowerBASIC code. I'll play with it some more, though, to see how to remove it from within the PHP script.


                                    • #19
                                      It has been a long time since I did some php code work.
                                      So I might be wrong on anything I say.
                                      I remember some unwatered changes to some bulit in php code. Here is where you will have to watch the versions of php used.
                                      I thought some of those changes had something to do with slashes.
                                      Being paranoid of inserted code myself.
                                      If you cannot slap the person on the wrist for using undesired characters. You may want to replace the characters with word names for the chacrater like. "[forwardslash]" for the slash character.
                                      Gary. If your going to be using other compiler code with PHP scripts. I highly suggest using what you know works and will not change. In other words. Don't leave it up to php versions to keep the same standards or even something broke. Use what you feel you can depend on.
                                      p purvis


                                      • #20
                                        Hi Paul,
                                        Yes, in the little reading I've done, I can see that some functions work only in specific versions of PHP. What you say is good advice.