I’m reading an XML data from a PHP input and i’m receving the number 1 instead of the XML data.
the PHP code for reading the XML Data from PHP input:
$xmlStr="";
$file=fopen('php://input','r');
while ($line=fgets($file) !== false) {
$xmlStr .= $line;
}
fclose($file);
the PHP code for sending the XML:
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;
}
no matter what XML i’m sending, the receiving end is getting the number 1 instead
of the XML data. any ideas?
any information regarding the issue would be greatly appreciated.
update
the following code works:
$xmlStr = file_get_contents('php://input');
but why my code doesn’t ?
why my code returns 1 instead of the actual xml ?
Though I’d suggest using
file_get_contents, too, to answer your question:Because of operator precedence the line
doesn’t work the way you want it to. The result of the comparison
fgets($file) !== falseis assigned to $line. When you append it to $xmlStr the boolean value is cast to string. Since the condition of the while loop is that $line is true(string)$linewill always be1“within” that loop.You’d need
to change the precedence