PHP 将值发布到 java servlet

发布于 2024-09-15 16:57:49 字数 1288 浏览 1 评论 0原文

我想将 php 文件中的一些值(例如布尔值)发布到我的 Java Servlet。

它基本上是一个我正在尝试实现的通知系统。其功能是用户将问题发布到网站(使用 php 构建),当他得到问题的答案时,我应该能够传递一个布尔值从我的 php 文件到 java servlet。

我为此使用的 php 函数是

函数 do_post_request($url, $data, $optical_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;
     }

这个方法总是回显“echo in if statements”。这意味着 fp 的值为 null,这对我来说意味着我提供的 URL 是错误的。

但我提供的 URL 基本上是一个 java servlet,我可以在我的网络浏览器中打开它。

那么任何人都可以帮助我吗?

我会要求详细解释建议的任何 php 代码修改,因为我是 php 新手。另外,请让我知道,以防我需要提供有关此问题的额外信息,

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-09-22 16:57:49

正如我在评论中指出的,我首先尝试回显该网址。为此,请在调用 fopen 之前添加 echo $url;。这将确认是否获取了正确的 url。

如果不是,那么您需要找出原因。

如果它获得正确的 url,则您安装的 php 可能未配置为与 fopen 的远程(基于 url)调用一起使用。您可以在此处。如果是这种情况,您只能在 php.ini 中更改它,这意味着如果您不在自己管理的盒子上,则您的主机必须允许它。

注意:我相信您可以通过在调用 phpinfo(); 显示的信息中查找 allow_url_fopen 来检查这一点

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文