Calling JSP page from PHP script

Hi All,

Hoping you can help, have spent many an hour and searching around and not finding much. I have a main php program running and I wish to make a call to execute a JSP page, sounds simple enough. I have tried include but did not work, header location but this causes header redirect problems and curl which does not work? Also tried file_get_contents, did not work either? I wish to stay in my main.php program and not get redirected, just make a call and successfully execute the JSP page running on another server? Hope this is clear, any help much appreciated.

this works…

[php]<?php
$homepage = file_get_contents(‘http://www.example.com/’);
echo $homepage;
?>[/php]

but this does not work

[php]<?php
$homepage = file_get_contents(‘http://hostname:8080/pagename.jsp?param1=value1&param2=value2/’);
echo $homepage;
?>[/php]

just times out…

have also tried CURL, also times out.

[php]
$URL =‘http://hostname:8080/pagename.jsp?param1=value1&param2=value2’;
//Initialize the Curl session
$ch = curl_init();
//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $URL);
//Execute the fetch
$data = curl_exec($ch) or die(curl_error($ch));
//Close the connection
curl_close($ch);
print $data;
[/php]

I don’t know of any reason why cURL wouldn’t work. If you copy the URL to your browser does it load?

Hi Matt,

Thanks for the reply.

Yes the URL works in a browser and I can get it to work using.

[php]…

ob_end_clean();
header(‘Location: http://example.com/page.jsp’);
exit();[/php]

But it will not work using file_get_contents or cURL, no idea why? I can get file_get_contents to work with other sites but not this URL. I am now checking all firewalls and access to the URL. Perhaps there is something in a simple http request that works that a cURL or file_gets_contents is not able to work? Any help much appreciated and thans for confirming cURL should work.

Thx, Digby.

If you can post the actual URL (or PM it to me) I’ll do some testing

This may be one of the following:

  • You are missing a cookie of some sort
  • The server is set to discard anything from curl (from user-agent)
  • The server is set to discard anything that does not send another specific header

Points #2 and #3 can be fixed by changing the user-agent that curl uses. This is easily done using curl_setopt();

Sponsor our Newsletter | Privacy Policy | Terms of Service