Php 为什么在添加标题后无法获取POSTFIELDS?
这是客户端中的代码:Php 为什么在添加标题后无法获取POSTFIELDS?,php,php-curl,Php,Php Curl,这是客户端中的代码: //Create XML Object $newsXML = new SimpleXMLElement("<news></news>"); $newsXML->addAttribute('newsPagePrefix', 'value goes here'); $newsIntro = $newsXML->addChild('content',"test data"); $newsIntro->addAttribute('type'
//Create XML Object
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content',"test data");
$newsIntro->addAttribute('type', 'latest');
// URL of api
$url = 'http://localhost:81/demophp/api2.php';
// init CURL
$ch = curl_init($url);
$headers = array(
'Content-type: application/xml',
'Authorization: 123456',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// exist return value
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// method: POST
curl_setopt($ch, CURLOPT_POST, 1);
// parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, ["xml"=>$newsXML->asXML()]);
$result = curl_exec($ch);
curl_close($ch);
如果我删除头,它可以得到xml。
但如果我把头加到卷曲上
出现无法获取xml的错误:
为什么在添加标题后无法获取POSTFIELDS?试试这个curl脚本。它可能对你有用
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content',"test data");
$newsIntro->addAttribute('type', 'latest');
// URL of api
$url = 'http://localhost:81/demophp/api2.php';
$input_xml = $newsXML->asXML();
// init CURL
$ch = curl_init($url);
$headers = array(
'Content-type: application/xml',
'Authorization: 123456'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// exist return value
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
// method: POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
// parameters
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
因为您将内容类型设置为application/xml,然后插入postfields而不是xml body,如何解决此问题?谢谢。我们不设置内容类型。或者将xml作为请求的原始正文。如果[或者将xml作为请求的原始正文],在api2.php中,如何获取xml字符串?如果将xml直接放入正文中,则可能需要将“获取”内容归档php://input';
$headers = array(
'Content-type: application/xml',
'Authorization: 123456',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Authorization: 123456
Notice: Undefined index: xml in D:\demophp\api2.php on line 8
Fatal error: Uncaught Exception: String could not be parsed as XML in
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content',"test data");
$newsIntro->addAttribute('type', 'latest');
// URL of api
$url = 'http://localhost:81/demophp/api2.php';
$input_xml = $newsXML->asXML();
// init CURL
$ch = curl_init($url);
$headers = array(
'Content-type: application/xml',
'Authorization: 123456'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// exist return value
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
// method: POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
// parameters
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
if($header=="Authorization")
echo "$header: $value";
}
$data = file_get_contents("php://input");
$xmlstr=$data;
$myfile = fopen("log.txt", "w") or die("Unable to open file!");
fwrite($myfile, $xmlstr);
fclose($myfile);
$xml= new SimpleXMLElement($xmlstr);
echo $xml['newsPagePrefix'];
exit;