php接受xml和发送(post)xml

接收xml:
$xml = file_get_contents(‘php://input’);
发送(post):
[php]
//获得用户信息函数
function getUserInfoQ($token,$sender,$key,$post_url){
$xml_data="<?xml version="1.0" encoding="UTF-8" ?>";
$xml_data.='<ecity>’;
$timpstamp=date(‘YmdHis’);
$xml_data.='<msgname>getuserinforeq</msgname><msgversion>1.0</msgversion><transactionid>’.$timpstamp.'</transactionid>’;
$xml_data.='<timestamp>’.$timpstamp.'</timestamp>’;
$xml_data.='<msgsender>’.$sender.'</msgsender>’;
$xml_content='<svccont><token>’.$token.'</token></svccont>’;
$xml_data.=$xml_content;
$keyMD5=md5($xml_content.$timpstamp.$key);
$xml_data.='<validatestring>’.$keyMD5.'</validatestring>’;
$xml_data.='</ecity>’;
//header ( "content-type:text/xml;charset=utf-8" );
//echo $xml_data;exit;
$header[] = "Content-type: text/xml";//定义content-type为xml
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $post_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(empty($response))
{
echo ‘端口不通’;
exit;
}
@curl_close($ch);
//header ( "content-type:text/xml;charset=utf-8" );
//echo $response;exit;
return $feed = @simplexml_load_string($response);
[/php]
或者:
[php]
$contentLength = strlen($XPost);
$fp = fsockopen("loct.test.cn", 80,$errno,$errstr,5);
if(!$fp){
return "不能连接到 server服务器";
}
fputs($fp, "POST /test/locOrderService HTTP/1.0rn");
fputs($fp, "Host: loct.test.cnrn");
fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");
fputs($fp, "Content-Length: $contentLengthrn");
fputs($fp, "Connection: closern");
fputs($fp, "rn");
fputs($fp, $XPost);
$result = ”;
while (!feof($fp)) {
stream_set_timeout($fp, 5);
$result .= fgets($fp, 128);
$fsocketinfo = stream_get_meta_data($fp);
if ($fsocketinfo[‘timed_out’]) {
break;
}
}
[/php]

发布日期:
分类:PHP

发表评论

邮箱地址不会被公开。 必填项已用*标注