ufk*_*ufk 2 php xml curl domdocument
我正在从PHP输入中读取XML数据,我正在接收数字1而不是XML数据.
用于从PHP输入读取XML数据的PHP代码:
$xmlStr="";
$file=fopen('php://input','r');
while ($line=fgets($file) !== false) {
$xmlStr .= $line;
}
fclose($file);
Run Code Online (Sandbox Code Playgroud)
用于发送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;
}
Run Code Online (Sandbox Code Playgroud)
无论我发送什么XML,接收端都会获得数字1而不是XML数据.有任何想法吗?
任何有关该问题的信息将不胜感激.
以下代码有效:
$xmlStr = file_get_contents('php://input');
Run Code Online (Sandbox Code Playgroud)
但为什么我的代码没有?为什么我的代码返回1而不是实际的xml?
虽然我也建议使用file_get_contents,但也要回答你的问题:
因为运算符优先于该行
while ($line=fgets($file) !== false)
Run Code Online (Sandbox Code Playgroud)
不按你想要的方式工作.比较结果fgets($file) !== false分配给$ line.当您将其附加到$ xmlStr时,布尔值将转换为字符串.由于while循环的条件是$ line为true,因此(string)$line将始终1在"循环"内.
你需要
while ( ($line=fgets($file)) !== false)
Run Code Online (Sandbox Code Playgroud)
改变优先顺序
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |