原创部分:
$tms = ''; //XML部分参数需按API要求组装
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://wsbeta.fedex.com:443/web-services",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $tms,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: text/xml",
),
));
$xmls = curl_exec($curl);
$err = curl_error($curl);
$aaaa = xml_to_array($xmls);
echo "
";
print_r($aaaa);die;function xml_to_array($xml){
// 创建解析器
$parser = xml_parser_create();
// 将 XML 数据解析到数组中
xml_parse_into_struct($parser, $xml, $vals, $index);
// 释放解析器
xml_parser_free($parser);
// 数组处理
$arr = array();
$t=0;
foreach($vals as $value) {
$type = $value['type'];
$tag = $value['tag'];
$level = $value['level'];
$attributes = isset($value['attributes'])?$value['attributes']:"";
$val = isset($value['value'])?$value['value']:"";
switch ($type) {
case 'open':
if ($attributes != "" || $val != "") {
$arr[$t]['tag'] = $tag;
$arr[$t]['attributes'] = $attributes;
$arr[$t]['level'] = $level;
$t++;
}
break;
case "complete":
if ($attributes != "" || $val != "") {
$arr[$t]['tag'] = $tag;
$arr[$t]['attributes'] = $attributes;
$arr[$t]['val'] = $val;
$arr[$t]['level'] = $level;
$t++;
}
break;
}
}
return $arr;
}
转载部分:
// 首先检测是否支持curl if (!extension_loaded("curl")) { trigger_error("对不起,请开启curl功能模块!", E_USER_ERROR); } // 构造xml数据 $xmlData = "
"; $url = 'http://web.whm.com/getXml.php'; //接收xml数据的文件 $ch = curl_init(); // 初始一个curl会话 $timeout = 30; // php运行超时时间,单位秒 curl_setopt($ch, CURLOPT_URL, $url); // 设置url curl_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; wxf8b4f85f3a794e77 1001 错误描述 transaction_id=33534453534 1393860740 f8164781a303f4d5a944a2dfc68411a8c7e4fbea sha1 //接收传送的数据 $xml = file_get_contents("php://input"); //将xml数据写入文本文件"whm.txt"中 $handle =fopen('whm.txt','w'); fwrite($handle,$xml);