为什么我从在浏览器地址栏调用中工作的PHP WEB服务中得到一个空返回值


Why am I getting an empty return value from PHP WEB Service that works in a browser address bar call?

好吧,首先,我一定错过了什么,所以我为可能是新手的问题道歉。。。

我有一段复杂的代码不起作用,所以我把它放在这里或任何指针上。我不能分享太多,因为它是专有的,所以开始吧。

我有三层:用户、服务器和设备。服务器和设备都启用了php,客户端要么是IE,要么是Chrome——行为是一样的。

用户层将数据从HTML5表单发送到服务器,然后服务器将数据记录在数据库中,并可以发送到设备——在这里一切正常。

由于设备未启用https,我正在尝试设置触发器/响应模型。这意味着向设备发送一个缩写消息或密钥(作为GUID),然后设备回调到服务器以获取XML消息进行处理。回叫是使用get_file_contents()调用完成的。

所有部分似乎都在工作,服务器响应正在检索XML,客户端正在正确地选择XML标头——然而,当设备执行调用时,响应是空的。

$result = file_get_contents($DestURL) ;
// If I call the value in $DestURL in a browser address 
// box - it all works
// If I echo the $result, it is empty, and then nothing 
// executes, except the last line.
if (strlen($result)== 0 ) {
    // ==> this is not executing <==
    $msg = "Failed to open the <a href='" . htmlspecialchars($DestURL) . "'> URL<a>: " . htmlspecialchars($DestURL);
    $result="Error";
}
// ==> this is not executing <<==
if ($result=="Error") 
    {
    /*
     * need to send an error message
     */
    }
else 
    {
    $result = PrintMessage($my_address, $result 
    }
// ==> This is executing <==
Echo "all Finished";
?>

非常感谢任何人的任何想法。

服务器Web服务如下所示:

<?php
   header("Content-type: text/xml");
   // a bunch of items getting the data from the database
   $result = mysqli_query($con, $sql);
   $row = mysqli_fetch_array($result);
   echo $row['message_XML'];
?>

我仍然没有发生这种情况的真正原因,但一篇相关的帖子在这里提供了帮助:PHP ini file_get_contents外部url帮助很大-感谢这两个响应。

我已经将get从使用file_get_contents()更改为CURL调用。问题已解决。

这是代码:

function get_message($URL)
{
  /*
   * This code has been lifted from stackoverflow: URL to the article
  */
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_URL, $URL);
  // Can also link in security bits here.
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}