In the process of learning PHP, you will encounter when PHP sends XML data through curl. This article will explain the related methods.
In php programming, it is often used to transmit data in xml format, such as calling third-party interfaces such as WeChat. Here is a demonstration of php sending xml in curl form and receiving it through the server
1. Send xml data - postXml.php
wxf8b4f85f3a794e77 1001 错误描述 transaction_id=33534453534 1393860740 f8164781a303f4d5a944a2dfc68411a8c7e4fbea sha1 "; $url = 'http://web.whm.com/getXml.php'; //接收xml数据的文件$ch = curl_init(); // 初始一个curl会话$timeout = 30; // php运行超时时间,单位秒curl_setopt($ch, CURLOPT_URL, $url); // 设置urlcurl_setopt($ch, CURLOPT_POST, 1); // post 请求curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:text/xml; charset=utf-8")); // 一定要定义content-type为xml,要不然默认是text/html!curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);//post提交的数据包curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // PHP脚本在成功连接服务器前等待多久,单位秒curl_setopt($ch, CURLOPT_HEADER, 0); $result = curl_exec($ch); // 抓取URL并把它传递给浏览器// 是否报错if(curl_errno($ch)) { print curl_error($ch); } curl_close($ch); // //关闭cURL资源,并且释放系统资源 echo $result;
php sends XML data through curl and obtains XML data
2. Receive xml data - getXml.php
Copy after login
php sends XML data through curl and obtains XML data
3. Notes
When constructing xml, be sure to pay attention to the correct format, no spaces, etc.
Be sure to define content-type as xml, otherwise the default is text/html
This article explains how PHP sends XML data through curl and obtains XML data. For more related knowledge, please pay attention to the PHP Chinese website.
Related recommendations:
PHP perfectly generates word documents, and html elements can be added
ThinkPhp caching principle and detailed explanation
Discuz!X/Database DB:: Function operation method
The above is the detailed content of PHP sends XML data through curl and gets XML data. For more information, please follow other related articles on the PHP Chinese website!