Load text from external servlet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Greffin
Forum Newbie
Posts: 7
Joined: Tue May 12, 2009 3:49 am

Load text from external servlet

Post by Greffin »

Hi.

I'm making a portal page to display info on several different sites we manage. Each site has a servlet installed. When invoked given a specific query parameter, the servlet will output a short text string which I want to display on my portal.

This has previously been done by making 'include' calls to the servlets before any html are displayed to the user.

Code: Select all

 ...
    if (url_exists("http://".$appServer.":".$appPort."/".$appName."/")) {
      include("http://".$appServer.":".$appPort."/".$appName."/servlet/".$servlet."?param=VERSION");
    } 
... 
which gives me a string like '2.9.1.1.0'.

I've just recently switched to making separate AJAX calls to a php file - one for each servlet to be called. This should make the page load faster for the user, since fetching the information from the servlets will be asynchroneous.

The problem is that if I recode the above to:

Code: Select all

 <?
  $url  = $_GET['url'];
  include($url."?param=VERSION);
?> 
I get the following error outputted in my AJAX callback function:

Code: Select all

 <br />
<b>Warning</b>:  main(http://server.domain/appName/servlet/se ... er=SITE_ID
): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in <b>e:\oracle\10gas\apache\apache\htdocs\portal2_getinfo.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>:  main(): Failed opening 'http://server.domain/appName/servlet/servletName?parameter=SITE_ID' for inclusion (include_path='.;c:\php4\pear') in <b>e:\oracle\10gas\apache\apache\htdocs\portal2_getinfo.php</b> on line <b>2</b><br /> 
My portal resides in an Oracle 10gAS apache container, which comes with PHP4 preinstalled. I have been unable to upgrade to php5. There are no curl functionality installed either.

As you've probably guessed, I'm a real novice in PHP. Can anybody help me out here?
Last edited by Benjamin on Tue May 12, 2009 11:28 am, edited 1 time in total.
Reason: Changed code type from text to php.
Greffin
Forum Newbie
Posts: 7
Joined: Tue May 12, 2009 3:49 am

Re: Load text from external servlet

Post by Greffin »

...Bump...

Can anybody please tell me why include works when generating the page server side, but not in an asynchronous call? I would really like for this to work.

If I cannot use include, please inform me of alternatives. I've tried accessing these servers directly from AJAX, but I get an access denied. Googling this, I find that cross domain calls of this kind is a security violation and is blocked by the browser.

Please help!
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Load text from external servlet

Post by Griven »

The variable $_GET['url'] won't return the entire URL. It will only return the last portion of this address:

http://www.domain.com/index.php?[b]url[/b]=yourvariable

Take a look at this page and let me know if it helps: http://www.webcheatsheet.com/PHP/get_cu ... ge_url.php
Greffin
Forum Newbie
Posts: 7
Joined: Tue May 12, 2009 3:49 am

Re: Load text from external servlet

Post by Greffin »

Griven wrote:The variable $_GET['url'] won't return the entire URL. It will only return the last portion of this address:

http://www.domain.com/index.php?url=yourvariable
I see we have a misunderstanding here. Let me clarify:
My portal runs on http://myserver/
The page lists some servers in which we should collect information from:

Code: Select all

SERVER,      URL for info,                                          Returned VERSION
oneserver,   http://oneserver/servlet/myServlet?param=VERSION   -> 2.9.1.1.0
twoserver,   http://twoserver/servlet/myServlet?param=VERSION   -> 2.8.1.1.13
threeserver, http://threeserver/servlet/myServlet?param=VERSION -> 2.9.1.0.0
... and so on.

I want to, from http://myserver/, fetch the info from the URL's listed, and display them on my page in a grid. The problem is that I cannot read the data from these url's.

The url's are sent from the portal page, through an AJAX call to myserver/get_info.php, using the url to read as a GET parameter: http://myserver/get_info.php?url=http://oneserver/servlet/myServlet.
In get_info.php, I have the following routine:

Code: Select all

<?
   $url  = $_GET['url'];
   include($url."?param=VERSION);
?>
Which worked fine when loading the entire page from php, but when used from an AJAX call from the client, this produces the error message described in the initial post.

I've tried to go around the call to get_info.php, and point the AJAX call directly to each servlet, but this is cross domain calls which violates browser security. I have to read the data server side and push it out to my portal page.

I hope this clarifies things.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Load text from external servlet

Post by Griven »

That does clarify things. As I don't know much Javascript, I don't think I can help you with the technical hurdles. However, I noticed a couple of things in the code and error you posted. I don't know if you copied and pasted from your actual code or not, but if you did, you should take a look.

You posted

Code: Select all

include($url."?param=VERSION);
twice. It's missing the closing double quote.

Also, the URL that it failed to open:

Code: Select all

'http://server.domain/appName/servlet/servletName?parameter=SITE_ID'
doesn't match the "param" variable that you mentioned elsewhere. Perhaps it's trying to call "parameter" when it should be looking for "param"?

If neither one of those help, I'm sorry.
Post Reply