0

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

2
  • To check the url, echo it somewhere. This will confirm whether or not it is going where you think it is going. Commented Aug 23, 2010 at 3:59
  • I echoed the url.This confirmed me that i am getting the right url Commented Aug 24, 2010 at 16:49

1 Answer 1

1

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();

Sign up to request clarification or add additional context in comments.

1 Comment

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.