首页 > 开发 > PHP > 正文

php 接收与发送xml文件

2024-05-04 21:58:40
字体:大 中 小
来源:转载
供稿:网友
  1. //接收xml: 
  2. $xml = file_get_contents('php://input'); 
  3.   
  4. //发送(post): 
  5. $xml_data = <xml>...</xml>"; 
  6. $url = http://dest_url; 
  7. $header[] = "Content-type: text/xml";//定义content-type为xml 
  8. curl_setopt($ch, CURLOPT_URL, $url); 
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
  11. curl_setopt($ch, CURLOPT_POST, 1); 
  12. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
  13. $response = curl_exec($ch); 
  14. if(curl_errno($ch)) 
  15. { 
  16.     print curl_error($ch); 
  17. } 
  18. curl_close($ch);  
  19.  //开源代码Vevb.com 
  20. //或者: 
  21. $fp = fsockopen($server, 80); 
  22. fputs($fp, "POST $path HTTP/1.0rn"); 
  23. fputs($fp, "Host: $serverrn"); 
  24. fputs($fp, "Content-Type: text/xmlrn"); 
  25. fputs($fp, "Content-Length: $contentLengthrn"); 
  26. fputs($fp, "Connection: closern"); 
  27. fputs($fp, "rn"); // all headers sent 
  28. fputs($fp, $xml_data); 
  29. $result = ''; 
  30. while (!feof($fp)) { 
  31. $result .= fgets($fp, 128); 
  32. } 
  33. return $result; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表