PHP sends XML data through curl and gets XML data

jacklove
Release: 2023-03-27 10:32:02
Original
4104 people have browsed it

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

<?php
 // 首先检测是否支持curlif (!extension_loaded("curl")) {
    trigger_error("对不起,请开启curl功能模块!", E_USER_ERROR);
} 
// 构造xml数据$xmlData = "
<xml>
<AppId>wxf8b4f85f3a794e77</AppId>
<ErrorType>1001</ErrorType>
<Description>错误描述</Description>
<AlarmContent>transaction_id=33534453534</AlarmContent>
<TimeStamp>1393860740</TimeStamp>
<AppSignature>f8164781a303f4d5a944a2dfc68411a8c7e4fbea</AppSignature>
<SignMethod>sha1</SignMethod>
</xml>";
 
 
$url = &#39;http://web.whm.com/getXml.php&#39;; //接收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;
Copy after login


php sends XML data through curl and obtains XML data

PHP sends XML data through curl and gets XML data

2. Receive xml data - getXml.php

<?php
 //接收传送的数据$xml = file_get_contents("php://input"); 
//将xml数据写入文本文件"whm.txt"中$handle =fopen(&#39;whm.txt&#39;,&#39;w&#39;);
 
fwrite($handle,$xml);
Copy after login


php sends XML data through curl and obtains XML data

PHP sends XML data through curl and gets 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!