点击登录
  • 欢迎访问无限星辰技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏无限星辰吧
  • 好集导航开张了,传送门:好集导航

php接受xml和发送(post)xml 接收返回值转换为json

PHP学习笔记 crx349 5926次浏览 0个评论 扫描二维码

接收xml:

1
$xml = file_get_contents('php://input');

发送(post):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$xml_data = <xml>...</xml>";
$url = http://dest_url;
$header[] = "Content-type: text/xml";//定义content-type为xml
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);

或者:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$fp = fsockopen($server, 80);
fputs($fp, "POST $path HTTP/1.0\r\n");
fputs($fp, "Host: $server\r\n");
fputs($fp, "Content-Type: text/xml\r\n");
fputs($fp, "Content-Length: $contentLength\r\n");
fputs($fp, "Connection: close\r\n");
fputs($fp, "\r\n"); // all headers sent
fputs($fp, $xml_data);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;
 ---------------------------------------------------------------------------------
 
 
//xml提交获取返回值
 
//构造xml
 $xml="< ?xml version="1.0" encoding="UTF-8"?>
 <request>
 
<ip>127.0.0.1</ip>
 
 </request>";
 
$url = 'http://192.168.1.188';//接收XML地址
 
$header = 'Content-type: text/xml';//定义content-type为xml
 
$ch = curl_init(); //初始化curl
 
curl_setopt($ch, CURLOPT_URL, $url);//设置链接
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
 
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header, 'Content-length: ' . strlen($xml)));//设置HTTP头
 
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST数据
 
$response = curl_exec($ch);//接收返回信息
 
curl_close($ch); //关闭curl链接
 
//转换为数组
 $xml_array = simplexml_load_string($response);
 
//变成json格式
 $json = json_encode($xml_array);
 
echo $json;//显示返回信息

无限星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明php接受xml和发送(post)xml 接收返回值转换为json!
喜欢 (1)
[]
分享 (0)

您必须 登录 才能发表评论!