php调用电信isag的方法_网络小农_新浪博客

php在创建或调用webservice方面太差了,这几天在调用电信isag接口发送短信的时候是深有体会啊,当时因为懒得装java的运行环境放弃使用java改用php,现在想想太后悔了。下面是php通过socket调用isag接口发送短信的例子:

function sms_url_http($sms_content,$phone){

$http_rs = sms_send(‘127.0.0.1’,

****,$sms_content,$phone);

 

$doc = new DOMDocument();

 

$doc->loadXML($http_rs);

   

 $errecords =

$doc->getElementsByTagName_r(“ServiceException”);

$rettxt = ”;

foreach( $errecords as $errecord ){

$messageid =

$errecord->getElementsByTagName_r(‘messageId’);

$tagtxt =

$errecord->getElementsByTagName_r(‘text’);

$rettxt =

$messageid->item(0)->nodeValue.’=’.$tagtxt->item(0)->nodeValue;

}

$errecords =

$doc->getElementsByTagName_r(“sendSmsResponse”);

foreach( $errecords as $errecord ){

$rettxt = $errecord->nodeValue;

}

print_r($rettxt);

return $rettxt;

}

function sms_send($host, $port,$msg_content,$phone) {

$timeout = 80;

$sock = fsockopen($host, $port, $errno, $errstr,

$timeout);

if (!$sock){

return “$errno|$errstr”; //die(“$errstr ($errno)\n”);

}

date_default_timezone_set(“PRC”);

$timestamp = date(“mdHis”);

$spPwd = md5(‘*********’.$timestamp);

$msg = ‘<?xml version=”1.0″

encoding=”UTF-8″?><soapenv:Envelope

xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”

xmlns:xsd=”http://www.w3.org/2001/XMLSchema”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”><soapenv:Header><ns1:RequestSOAPHeader

soapenv:actor=”http://schemas.xmlsoap.org/soap/actor/next”

soapenv:mustUnderstand=”0″

xmlns:ns1=”http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1″><ns1:spId>SPID</ns1:spId><ns1:timeStamp>’.$timestamp.'</ns1:timeStamp><ns1:spPassword>’.$spPwd.'</ns1:spPassword><ns1:OA>tel:+86′.$phone.'</ns1:OA><ns1:FA>tel:+86′.$phone.'</ns1:FA><ns1:multicastMessaging>false</ns1:multicastMessaging></ns1:RequestSOAPHeader></soapenv:Header><soapenv:Body><sendSms

xmlns=”http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local”><addresses>tel:+86′.$phone.'</addresses><senderName>接入号</senderName><message>’.$msg_content.'</message></sendSms></soapenv:Body></soapenv:Envelope>’;

fwrite($sock, “POST /SendSmsService HTTP/1.0\r\n”);

fwrite($sock,”Content-Type: text/xml;

charset=utf-8\r\n”);

fwrite($sock, “Accept: application/soap+xml, application/dime,

multipart/related, text/*\r\n”);

fwrite($sock, “User-Agent: Axis/1.4\r\n”);

fwrite($sock, “Host: $host:$port\r\n”);

fwrite($sock,”Cache-Control: no-cache\r\n”);

fwrite($sock,”Pragma: no-cache\r\n”);

fwrite($sock,”SOAPAction: \”\”\r\n”);

fwrite($sock,”Content-Length: “.strlen($msg).”\r\n”);

fwrite($sock,”\r\n”);

fwrite($sock, $msg);

$headers = “”;

while ($str = trim(fgets($sock, 4096))){

$headers .= “$str\n”;

}

$body = “”;

while (!feof($sock))

$body .= fgets($sock, 4096);

fclose($sock);

return $body;

}

来源URL:http://blog.sina.com.cn/s/blog_4f91d7940100u792.html