簡體   English   中英

為什么我在瀏覽器地址欄調用中從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-行為相同。

用戶層將數據從HTML 5表單發送到服務器,服務器再將其記錄在數據庫中,並可以發送到設備-一切正常。

由於設備未啟用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";
?>

任何人的任何想法都非常感謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM