现在的位置: 首页 > 综合 > 正文

PHP CURL 发送和接收XML数据,并用$HTTP_RAW_POST_DATA接收

2018年04月12日 ⁄ 综合 ⁄ 共 948字 ⁄ 字号 评论关闭

PHP CURL 发送和接收XML数据,并用$HTTP_RAW_POST_DATA接收

PHP代码
  1. <?php  
  2.   
  3. $xml = file_get_contents(‘test.xml’);  
  4.   
  5. $header[]="Content-Type: text/xml; charset=utf-8";  
  6.   
  7. $header[]="User-Agent: nginx/1.0.0";  
  8. $header[]="Host: 127.0.0.1";  
  9. $header[]="Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";  
  10. $header[]="Connection: keep-alive";  
  11. $header[]="Content-Length: ".strlen($xml);  
  12.   
  13. $url = "http://{$_SERVER['HTTP_HOST']}".dirname($_SERVER['PHP_SELF']).‘/response.php’;  
  14.   
  15. $ch = curl_init();  
  16. curl_setopt($ch, CURLOPT_URL, $url);  
  17. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
  18. curl_setopt($ch, CURLOPT_POST, 1);   
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);  
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  21. curl_setopt($ch, CURLOPT_HEADER, 0);  
  22. $res = curl_exec($ch);  
  23. curl_close($ch);  
  24.   
  25. header(‘Content-Type:text/xml; charset=utf-8′);  
  26. echo ($res);  
  27.   
  28. ?>  

 

response.php 文件内容:

PHP代码
  1. <?php   
  2. echo $HTTP_RAW_POST_DATA;  
  3. ?>  

抱歉!评论已关闭.