Google Answers Logo
View Question
 
Q: Visual Basic 6.0 - how to execute a remote PHP script? ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Visual Basic 6.0 - how to execute a remote PHP script?
Category: Computers > Programming
Asked by: g8z-ga
List Price: $10.00
Posted: 03 Feb 2003 15:43 PST
Expires: 05 Mar 2003 15:43 PST
Question ID: 156931
hi,

Please first read this thread:

http://www.a1vbcode.com/vbforums/topic.asp?topic_id=5559&forum_id=1&Topic_Title=newbie+question+%2D+using+OpenURL&forum_title=General+Visual+Basic&M=True&S=True

I need a more complete answer to the question that I posed on this
online forum. I essentially need a way to execute a PHP page from
within Visual Basic 6. My Visual Basic program is not a form-based
program, but rather an executable that runs in the background, so
after executing the PHP code on the remote server, it should *not*
open the web browser.

I don't need complete source code - just a tip to set me in the right
direction.

Thanks,
Darren
Answer  
Subject: Re: Visual Basic 6.0 - how to execute a remote PHP script?
Answered By: mrbuzz-ga on 03 Feb 2003 16:41 PST
Rated:5 out of 5 stars
 
Hi g8z-ga,

I understand that you just want a program that triggers your PHP
script without displaying any sort of GUI to the user.  Unfortunately,
this cannot be done without a form.  Even if you created a module only
application, there would still be a console window.

The solution that I've used many times in the past to accomplish
exactly what you've described uses a hidden form so that the user will
never see anything.  Basically, you add the Internet Transfer
component to the form and have the following code in Form_Load():

Private Sub Form_Load()
    Me.Width = 0
    Me.Height = 0
    Me.Left = -2000
    Me.Top = -2000
    Me.Show
    Me.Hide

    Dim blah As String
    On Error Resume Next 'to avoid http errors
    blah = Inet1.OpenURL("http://www.yourdomain.com/phpscript.php")

    End
End Sub


This will effectively execute your PHP script without displaying a GUI
to the user and will end itself once it's done.

I hope this is what you were looking for.  If you have any questions,
please use the request clarification button to contact me.

Good luck,
mrbuzz-ga

Request for Answer Clarification by g8z-ga on 04 Feb 2003 11:25 PST
hi,
Thanks for the answer. Your suggestion should be fine, except that the
actual call to OpenURL really needs to be in a class function instead
of the Form_Load function for my purposes. I tried modifying the code
to this:

Public Sub Form_Load()

    Me.Width = 0
    Me.Height = 0
    Me.Left = -2000
    Me.Top = -2000
    Me.Show
    Me.Hide
 
    Dim FTPBean As New FTPBean
    FTPBean.start (Me.Inet1)
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''
Then, within the FTPBean class start method I have...
''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function start(ByRef INetObject)
    On Error Resume Next
    INetObject.openURL ("http://www.mydomain.com/phpfile.php")

    ' more code here
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''
When I call the openURL function from within Form_Load, everything
works perfectly, but when I pass the Inet1 object into start, it
doesn't work. The other code in start is executed just fine, and when
I debug it, I'm finding that the Inet1 object becomes "" as soon as
it's passed to the start function as InetObject. I initially thought
this was a pass by reference vs. pass by value problem, so I added
ByRef, but that didn't help.

How can I modify this so that OpenURL can be used within a module or
class, which is what I really need?

Clarification of Answer by mrbuzz-ga on 04 Feb 2003 11:35 PST
Ah, I see.  OK, gimme a few minutes and let me see what I can do.

Thanks,
mrbuzz-ga

Clarification of Answer by mrbuzz-ga on 04 Feb 2003 11:42 PST
OK, I got it.  This following code works for me:

Public Sub Form_Load()
 
    Me.Width = 0
    Me.Height = 0
    Me.Left = -2000
    Me.Top = -2000
    Me.Show
    Me.Hide
  
    Dim FTPBean As New FTPBean
    MsgBox FTPBean.start(Me.Inet1)

End Sub


'in ftpbean class
 
Public Function start(ByRef INetObject)
    On Error Resume Next
    Dim blah
    blah = INetObject.openURL("http://www.mydomain.com/phpfile.php")
    start = blah
    ' more code here
End Function


Perhaps the reason why yours didn't work is maybe you didn't patch
VB6.  I'm currently running VB6 SP5 which you can download at (
http://download.microsoft.com/download/vstudio60ent/SP5/Wideband-VB/WIN98Me/EN-US/vs6sp5vb.exe
) it's about 57mb dl.  The Microsoft Internet Transfer Control is at
SP4 in the VB6 SP5 patch.

Let me know if this works/doesn't work.  Thanks.

Regards,
mrbuzz-ga

Request for Answer Clarification by g8z-ga on 04 Feb 2003 13:33 PST
I downloaded and installed the patch, and rebooted my system, but it
didn't help. I also checked the Components and References that were
being added to the project, and it appears that everything that I need
is there.

I've taken a few screenshots of my debugging environment. One
breakpoint was set in the Form_Load method, and one was set in the
start method of FTPBean. It seems that the INetObject is being lost
somehow in the call to the start method (it's no longer the Inet1
object that it should be, but instead, it morphs into "").

The URL for the screenshots:
http://www.tufat.com/screenshots.html

I think it may be a scoping issue, or something in my project
environment that is not allowing objects to be passed correctly,
although I have no idea what it may be. Within the start method I have
been able to pass variables to private functions and return their
results just fine, however.

Clarification of Answer by mrbuzz-ga on 04 Feb 2003 15:07 PST
Hmmm, that's really odd.  For me, the code I've used just spits out
the output of the HTML of the page.  If you haven't tried this
already, make a new project and do EXACTLY the following:

1. Add component, MS Internet Transfer Control (SP4).
2. Place the control onto Form1, without modifying any of its
properties.
3. In Form1, View Code, paste the following (overwrite the existing
Form_Load):
-----------------------------------------------
Public Sub Form_Load() 
  
    Me.Width = 0 
    Me.Height = 0 
    Me.Left = -2000 
    Me.Top = -2000 
    Me.Show 
    Me.Hide 
   
    Dim FTPBean As New FTPBean 
    MsgBox FTPBean.start(Me.Inet1) 
 
End Sub
-----------------------------------------------

4. Add a new Class module, change Name to FTPBean.
5. In the class module, paste the following:
-----------------------------------------------
Public Function start(ByRef INetObject) 
    On Error Resume Next 
    Dim blah 
    blah = INetObject.openURL("http://www.mydomain.com/phpfile.php") 
    start = blah 
End Function 
-----------------------------------------------

5. Run the program as it is.


I've just followed the steps above myself to verify and I get a msgbox
with the HTML code for some 404 page on mydomain.com.  If that doesn't
work for some reason, perhaps you can try to call the Inet object
directly from your class, rather than passing it by reference using
Form1.Inet.OpenURL from the FTPBean class.  Also, I wanted to verify
we have identical DLL versions for VB6 and the Inet control.

My MSVBVM60.DLL is 6.0.92.37 with May 29, 2001 in the 'Comments' item
under version properties.
My MSINET.OCX is 6.0.88.62 with May 11, 2000 in the 'Comments' item
under version properties.
My stdole32.tlb is 2.10.3027.1.  All of these files are in
C:\Windows\system32 or C:\WINNT\system32 (depending on your OS).

I am running this on Windows XP SP1.  Hopefully we'll get to the
bottom of this.

Thanks,
mrbuzz-ga

Request for Answer Clarification by g8z-ga on 07 Feb 2003 17:04 PST
It must be my installation of VB. I tried the steps that you suggested
and voila!, it worked. But after a while, it mysteriously stopped
working and the Inet1 variable was being reset to "" again. Wierd.
Your solution obviously works, however. I ultimately got around the
problem by simply not passing the variable at all - instead, I'm
calling Inet1.openURL within the Form_Load function. Not really what I
wanted, but it works.

Clarification of Answer by mrbuzz-ga on 07 Feb 2003 17:34 PST
Hi g8z-ga,

First of all, thank you very much for the 5-star rating.  :)

Still, I wonder why it would go "".  Perhaps what you can try is using
something like RaiseEvent and having a function in your main app to
capture that event and run OpenURL with event parameters.  Though,
I've only done this with COM objects so I'm not entirely sure if it
will help in your situation.  Also the comment posted by momad-ga with
passing the object with "As Inet" might help too.

Regards,
mrbuzz-ga
g8z-ga rated this answer:5 out of 5 stars
He provided a valid solution to my problem, even though it didn't
actually work 100% on my system, due to installation issues with my
own copy of VB6. But answer was good, nonetheless.

Comments  
Subject: Re: Visual Basic 6.0 - how to execute a remote PHP script?
From: momad-ga on 04 Feb 2003 16:08 PST
 
Hey you two,
First of all, I wanted to say that the above code worked just fine for
me... so its confirmed that it works.. the problem is your specific
computer.  Second, have you tried the following:

 1.  Check for some variable in your FTPBean class with the same name
(INetObject)
 2.  Try this: Public Function Start(ByRef INetObject As Inet)
    Notice the "As Inet" part
 3.  If neither work, try the winsock2 control, you will not need a
form for it.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy