如何收到这个卷发帖?

时间:2012-05-21 20:32:45

标签: php xml post

我在服务器页面上需要做什么以及如何接收此xml文件?我被卡住了。 xml很好,用simplexml_load_string检查。

 $var='caca';
 $login_xml ='<xml>'.
         '<action>log_in</action>'.
     '<parameters>'.
         '<username>'.$var.'</username>'.
     '<pass>abdef01</pass>'.
     '</parameters>'.
     '</xml>';

 $URL = "http://myurl.com/login.php/";


 $ch = curl_init($URL);
 curl_setopt($ch, CURLOPT_MUTE, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
 curl_setopt($ch, CURLOPT_POSTFIELDS, "$login_xml");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($ch);
 curl_close($ch);

感谢您的时间。

2 个答案:

答案 0 :(得分:2)

由于数据是以text / xml而不是url编码的form-data发送的,因此不能使用$ _POST。您必须从php://输入流

中读取原始请求
$xml = file_get_contents('php://input');

答案 1 :(得分:0)

您的CURLOPT_POST_FIELDS出错: 此选项必须是数组:

...
$data = array('xml' => $login_xml);
...

然后:

...
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
...

这会将值'xml'作为帖子字段发送;

希望这有帮助。