php中調用WebService接口

1、背景

調用第三方短信提供商的WebService接口。php


2、介紹

1.WebService三要素:

SOAP(Simple Object Access Protocol) 用來描述傳遞信息的格式html

WSDL(Web Services Description Language) 用來描述如何訪問具體的接口web

UDDI(Universal Description Discovery and Integration) 用來管理,分發,查詢WebService工具

2.SoapUi

SoapUI是一個開源測試工具,經過soap/http來檢查、調用、實現Web Service的功能/負載/符合性測試。測試


3、使用方法

1.創建SOAP

使用SoapUi:ui

使用php:code

$client = new SoapClient("http://XXX/webservice/mmsservice.asmx?wsdl",
        array(
            "stream_context" => stream_context_create(
                array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                    )
                )
            )
        )
    );

2.調用接口 and 獲取返回值

使用SoapUi:htm

使用php:blog

//調用接口
    $parm = array('mobile' => ‘136XXXXXX’, 'mmsid' => 'XXX', 'sToken' => 'XXX');
    $result = $client->SendPersonMMS($parm);

    //獲取返回值
    $result = get_object_vars($result);  
    echo $result["SendPersonMMSResult"];

4、整體代碼

header("content-type:text/html;charset=utf-8");
try {

    //解決OpenSSL Error問題須要加第二個array參數,具體參考 http://stackoverflow.com/questions/25142227/unable-to-connect-to-wsdl
    $client = new SoapClient("http://XXX/webservice/mmsservice.asmx?wsdl",
        array(
            "stream_context" => stream_context_create(
                array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                    )
                )
            )
        )
    );
    //print_r($client->__getFunctions());
    //print_r($client->__getTypes());

    $parm = array('mobile' => ‘136XXXXXX’, 'mmsid' => 'XXX', 'sToken' => 'XXX');
    $result = $client->SendPersonMMS($parm);
    //print_r($result);

    //將stdclass object的$result轉換爲array
    $result = get_object_vars($result);  
    //輸出結果 
    echo $result["SendPersonMMSResult"];
   

} catch (SOAPFault $e) {
    print $e;
}
相關文章
相關標籤/搜索