使用PHP的soap擴充套件, 進行wdsl操作

2021-06-22 18:58:17 字數 3638 閱讀 5595

本文是乙個使用php soap訪問webservice的例子.

wsdl檔案請見 這個**. 裡面有很多好用的伺服器.

大部分可以免費使用, 有一部分要收費.

以中文簡繁轉換為例.

簡繁轉換的wsdl檔案位址為: 

下面是php**, 其中有說明.

<?php

$wsdl = '';

// 以wsdl方式, 例項化類.

$soap = new soapclient( $wsdl );

var_dump ( $soap->__getfunctions() ); // 這是此介面可以提供哪些服務, 就是函式定義, 有引數, 有返回值

var_dump( $soap->__gettypes() ); // 這是此介面使用的資料型別.

以下為上面例子的返回值:

函式宣告

array(size=4)

0 =>

string

'tosimplifiedchineseresponse tosimplifiedchinese(tosimplifiedchinese $parameters)'

(length=80)

1 =>

string

'totraditionalchineseresponse totraditionalchinese(totraditionalchinese $parameters)'

(length=83)

2 =>

string

'tosimplifiedchineseresponse tosimplifiedchinese(tosimplifiedchinese $parameters)'

(length=80)

3 =>

string

'totraditionalchineseresponse totraditionalchinese(totraditionalchinese $parameters)'

(length=83)

型別定義

array(size=4)

0 =>

string

'struct tosimplifiedchinese '

(length=45)

1 =>

string

'struct tosimplifiedchineseresponse '

(length=73)

2 =>

string

'struct totraditionalchinese '

(length=46)

3 =>

string

'struct totraditionalchineseresponse '

(length=75)

從這個輸出中, 可以看到, 

totraditionalchineseresponse totraditionalchinese(totraditionalchinese $parameters)
這是簡體轉換為繁體的函式.

引數 totraditionalchinese 

結構為:

struct totraditionalchinese

對應 於php, 就是乙個陣列, 以stext為鍵, 值為字串,

例如: array( 'stext' => '要轉成繁體的漢字' )

此函式的返回值為:

struct totraditionalchineseresponse

這對於php就是, 乙個stdclass, 有乙個叫做totraditionalchineseresult的屬性

下面是乙個有完整返回值的php**, 其中包含傳送的xml資訊和接收的xml資訊.

<?php 

// wsdl位址

$wsdl = '';

// 例項化, trace表示要記錄傳送接收的xml資料

$soap = new soapclient( $wsdl, array( 'trace' => true) );

// 直接呼叫這個函式, 因為在此物件中, 所有的函式都是以雙下劃線開頭, 所以不會有衝突.

$rs = $soap->totraditionalchinese( array( 'stext' => '我要轉成繁體哦, 龍' ));

// 顯示轉換以後的值, stdclass物件.

var_dump( $rs );

// 顯示轉換的內容, 字串

echo $rs->totraditionalchineseresult;

echo '以下為收發資訊';

// 傳送http頭

var_dump( $soap->__getlastrequestheaders() );

// 傳送的http_body

var_dump( $soap->__getlastrequest() );

// 接收的http頭

var_dump( $soap->__getlastresponseheaders() );

// 接收的http_body

var_dump( $soap->__getlastresponse() );

以下是上面這個例子的返回值:

object(stdclass)[2]

public 'totraditionalchineseresult' => string '我要轉成繁體哦, 龍' (length=26)

我要轉成繁體哦, 龍

以下為收發資訊

我要轉成繁體哦, 龍

string '<?xml version="1.0" encoding="utf-8"?>我要轉成繁體哦, 龍

' (length=420)

php進行soap呼叫

ret client call getsupportcity array callparams array byprovincename 安徽 var dump ret var dump client getfunctions 檢視所有可用方法 要檢視示例的介面,可以訪問 http www.webx...

php擴充套件安裝soap模組

首先安裝完php 後最好保留當時安裝的檔案,比如我的路徑 export1 soft cd php 5.2.8 ext soap usr local php bin phpize configure with php config usr local php bin php config enable...

使用gsoap進行soap開發

1 通過wsdl檔案生成.h檔案 用法 wsdl2h o 頭檔名 wsdl檔名或url wsdl2h常用選項 type map檔案用於指定soap xml中的型別與c c 之間的轉換規則,比如在wsmap.dat裡寫 2 由.h檔案生成所需要的cpp c檔案 用法 soapcpp2 標頭檔案例 so...