views:

30

answers:

1

Hi,

I would like to post some values (for example boolean) from a php file to my Java Servlet.

It is basically a notification system which i am trying to implement.The functionality is that a user posts a question to a website (built using php) and when ever he gets an answer to his question,i should be able to pass a boolean value from my php file to java servlet.

The php function i am using for this to work is

function do_post_request($url, $data, $optional_headers = null) {

        $params = array('http' => array(
               'method' => 'POST',
               'content' => $data
          ));

           if ($optional_headers!== null) {
            $params['http']['header'] = $optional_headers;
              }


             $ctx = stream_context_create($params);

             $fp = fopen($url, 'rb', false, $ctx);

             if (!$fp) {
             **echo '<br>echo in if statement <br>';**
            throw new Exception("Problem with $url, $php_errormsg");
             }


            $response = @stream_get_contents($fp); 

             if ($response === false) {
               throw new Exception("Problem reading data from $url, $php_errormsg");
                  }

            return $response;
     }

And this method always echoes "echo in if statement".That means the value of fp is null AND that means to me that the URL i provided is wrong.

But the URL which i provided is basically a java servlet and i can open it in my web browser.

So can anyone help me on this.

i would request a detailed explanation of any php code modifications suggested as i am new to php.Also please let me know incase i need to provide extra information about this question

Thanks

+1  A: 

As I noted in the comments, I would first try echoing the url. To do this, add echo $url; just before the call to fopen. This will confirm whether or not it is getting the right url.

If it is not, then you'll need to troubleshoot why not.

If it is getting the right url, there is a chance that your installation of php is not configured to work with remote (url-based) calls to fopen. You can get some details on that here. If this is the case, you can only change it in php.ini, which means that your host has to allow it if you're not on a box you administer yourself.

Note: I believe you can check for this by looking for allow_url_fopen in the information displayed by a call to phpinfo();

JGB146
The allow_url_fopen is ON for the local as well as the master.Since i am using http, default wrappers are provided for it.Correct me if i am wrong.So is there anything else which i need to take care of ?
bhargava