您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“如何用PHP實現Soap通訊”,內容詳細,步驟清晰,細節處理妥當,希望這篇“如何用PHP實現Soap通訊”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
代碼如下:
<? php function HttpSoap($server, $port, $url, $namespace, $action, $data) { $fp = @fsockopen($server, $port); if (!$fp) { return FALSE; } else { $soapData = ConstructData($namespace, $action, $data); $length = strlen($soapData);$out = "POST $url HTTP/1.1\r\n"; $out .= "Host: $server\r\n"; $out .= "Content-Type: text/xml; charset=utf-8\r\n"; $out .= "Content-Length: $length\r\n"; $out .= "SOAPAction: \"$namespace$action\"\r\n\r\n"; $out .= $soapData; $out .= "\r\n\r\n";fputs($fp, $out); stream_set_timeout($fp, 2);$header = ""; while($line = trim(fgets($fp))) { $header .= $line."\n"; } $dataPos = strpos($header, "Content-Length: ") + 16; $dataEnd = strpos($header, "\n", $dataPos); $dataLength = substr($header, $dataPos, $dataEnd - $dataPos); $data = ""; if($dataLength > 0) { $data = fread($fp, $dataLength); } fclose($fp); if(strlen($data) != $dataLength || $dataLength <= 0) { return FALSE; } return $data; } }function ConstructData($namespace, $action, $data) { $soapData = " <?xml version="1.0\" encoding=\"utf-8\"?>\r\n"; $soapData .= "<soap:Envelope xmlns:xsi=\http://www.w3.org/2001/XMLSchema-instance\ xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"\r\n'>http://schemas.xmlsoap.org/soap/envelope/\">\r\n"; $soapData .= " <soap:Body>\r\n"; $soapData .= " <$action xmlns=\"$namespace\">\r\n"; foreach($data as $name => $value) { $name = iconv("GBK","UTF-8",$name); $value= iconv("GBK","UTF-8",$value); $soapData .= " <$name>$value</$name>\r\n"; } $soapData .= " </$action>\r\n"; $soapData .= " </soap:Body>\r\n"; $soapData .= "</soap:Envelope>";return $soapData; }<? $data=array( 'user'=>'測試', //如果需要輸入二進制數據請采用BASE64編碼 'pass'=>'test' ); echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data); ?>
讀到這里,這篇“如何用PHP實現Soap通訊”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。