简体   繁体   中英

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

OK, firstly, I must be missing something, so I apologise for what may turn out to be a newb question...

I have a complicated bit of code that is just not working, so am putting it out here or any pointers. I can't share too much as it is proprietary, so here goes.

I have three tiers: User, server, appliance. The server, and appliance are php enabled, the client is either IE, or Chrome - the behavior is the same.

The user tier sends data from an HTML 5 form to the server, which in turn logs it in a database, and can send to the appliance - all OK here.

Due to the appliance not being https enabled I am trying to set up a trigger/response model. This means sending an abbreviated message, or key (as a GUID), to the appliance, and then the appliance calling back to the server for an XML message for processing. The call back is done using a get_file_contents() call.

All the parts seem to be working, the server response is retrieving the XML and the client is picking the XML headers correctly - however when the appliance is performing the call, the response is empty.

$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";
?>

Any ideas from anyone greatly appreciated.

The Server Web service reads like this:

<?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'];
?>

I still have no real reason why this is happening, however a related post helped over here: PHP ini file_get_contents external url helped a huge amount - Thanks to both responses.

I've changed the get from using file_get_contents() to a CURL call. Problem Solved.

Here is the code:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM