使用多个参数从PHP调用asp.net Web服务

时间:2012-03-14 22:50:13

标签: php web-services asmx

我正在使用一个在php页面中使用SoapClient类的方法来调用asp.net站点中的Web服务。

这是php代码。

$client = new SoapClient("http://testurl/Test.asmx?WSDL");

$params = array( 'Param1'  => 'Hello', 
                'Param2' => 'World!');

$result = $client->TestMethod($params)->TestMethodResult;

echo $result;

问题是,我只回到第一个参数(Param1)“Hello”,看起来Param2存在问题。 这是asp.net方法。

[WebMethod]
public string TestMethod(string Param1, string Param2) 
{
    return Param1 + " " +  Param2; 
}

在回复中获得Hello World!我错过了什么?

3 个答案:

答案 0 :(得分:21)

试试这样:

$client = new SoapClient("http://testurl/Test.asmx?WSDL");
$params->Param1 = 'Hello';
$params->Param2 = 'World!';    
$result = $client->TestMethod($params)->TestMethodResult;

答案 1 :(得分:1)

***********index.php******************
<?php
require_once("lib/nusoap.php"); 
 $client = new SoapClient("http://localhost:1966/ListAndishmandan/WebServiseFinal.asmx?WSDL");

    $params = array( 'Param1'  => 'Moslem', 
                    'Param2' => 'Ganji!');

    $result = $client->TestMethod($params)->TestMethodResult;

    print_r( $result);
    $params = array( 'Param1'  => 'Moslem', 
                    'Param2' => 'Ganji!');
echo "\n \r";
    $result2 = $client->ShowNameFamely($params)->ShowNameFamelyResult;

    print_r( $result2);
?>

    *******************WebServiseFinal.asmx?WSDL**************************
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

    /// <summary>
    /// Summary description for WebServiseFinal
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class WebServiseFinal : System.Web.Services.WebService {

        public WebServiseFinal () {

            //Uncomment the following line if using designed components 
           //InitializeComponent(); 
        }

        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }
        [WebMethod]
        public string TestMethod(string Param1, string Param2)
        {
            return Param1 + " " + Param2;
        }

        [WebMethod]
        public string ShowNameFamely(string Param1, string Param2)
        {
            return Param1 + " " + Param2;
        }

    }

答案 2 :(得分:1)

我正在谷歌搜索多参数调用。 所有的线程都没有告诉以下内容。 当php调用.asmx web服务时,参数的传递必须与Web服务中使用的变量匹配:

public string XYZ(string p, string q) 

网络服务电话必须是:

$params = array( "p"  => $name1,    "q" => $name2 );

p,q对必须在php调用中命名并澄清。