2013-01-21 17 views
2

我正在從PHP輸入中讀取XML數據,並且正在接收數字1而不是XML數據。php輸入的結果是1而不是XML數據

用於讀取PHP輸入的XML數據的PHP代碼:

  $xmlStr=""; 
      $file=fopen('php://input','r'); 
      while ($line=fgets($file) !== false) { 
       $xmlStr .= $line; 
      } 
      fclose($file); 

用於發送XML的PHP​​代碼:

public static function xmlPost($url,$xml) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); // set url to post to 
    curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable 
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 4s 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // add POST fields 
    curl_setopt($ch, CURLOPT_POST, 1); 
    $result=curl_exec ($ch); 
    return $result; 
} 

不管我送什麼XML,接收端獲取XML數據的數字1而不是 。有任何想法嗎?

有關該問題的任何信息將不勝感激。

更新

下面的代碼工作:

$xmlStr = file_get_contents('php://input'); 

但爲什麼我的代碼不? 爲什麼我的代碼返回1而不是實際的XML?

+1

你能試着'$ xmlStr =的file_get_contents( 'PHP://輸入') ;' –

+0

與file_get_contents我得到的XML ..我試圖找出這個代碼有什麼問題..我試圖用這種方式工作(我正在與我的代碼獲取1的客戶端我試圖找出原因..我會改變!==) – ufk

回答

5

雖然我建議使用file_get_contents也一樣,要回答你的問題:
由於operator precedence

while ($line=fgets($file) !== false) 

不工作,你希望它的方式。比較結果fgets($file) !== false被分配給$ line。當你將它附加到$ xmlStr時,布爾值將轉換爲字符串。由於while循環的條件是$行是真的,(string)$line將永遠是1「在該循環內」。
你會需要

while (($line=fgets($file)) !== false) 

改變優先

2

儘量把多餘的括號:

while (($line=fgets($file)) !== false) {...} 
相關問題