使用PHP调用具有多个参数的asp.net web服务

15

我在一个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个回答

26

试试这样:

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

一个快速的问题。我的代码没有工作的原因是它被传递为一个类型为数组的单个参数吗? - Felasfaw
5
你能告诉我TestMethodResult是从哪里来的吗? - Bhavin Rana
4
$params对象是什么?我该如何设置它? - Aryeh Armon

1
我正在搜索多参数调用的相关信息。 所有的帖子都没有提到以下内容。 当php调用.asmx web服务时,传递的参数必须与web服务中使用的变量匹配:
public string XYZ(string p, string q) 

Web服务调用应该类似于:

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

在 PHP 调用中,必须对 p、q 对进行命名和澄清。


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

    }

$result2 = $client->ShowNameFamely($params)->ShowNameFamelyResult;$result2 = $client->ShowNameFamely($params)->ShowNameFamelyResult; - M.Ganji

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接