php中调用WebService接口

一、背景

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


二、介绍

1.WebService三要素:

SOAP(Simple Object Access Protocol) 用来描述传递信息的格式

WSDL(Web Services Description Language) 用来描述如何访问具体的接口

UDDI(Universal Description Discovery and Integration) 用来管理,分发,查询WebService

2.SoapUi

SoapUI是一个开源测试工具,通过soap/http来检查、调用、实现Web Service的功能/负载/符合性测试。


三、使用方法

1.建立SOAP

使用SoapUi:

使用php:


    $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:

使用php:


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

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


四、总体代码


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;
}

原文地址:https://www.cnblogs.com/xjnotxj/p/6212143.html