2012-03-14 82 views
10

我正在使用一种方法在一个php页面中使用SoapClient类来调用一个asp.net站点中的一个web服务。从多个参数调用PHP的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)“你好”回来,似乎像Param2有问题。 这里是asp.net方法。

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

我错过了什么来得到Hello World!的回应?

回答

19

尝试这样的:

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

...它的工作!谢谢! – Felasfaw 2012-03-14 23:02:54

+0

一个简单的问题。我的代码没有工作的原因是B/C它是作为一个单一的参数类型数组传递? – Felasfaw 2012-03-14 23:08:22

+0

@ Felasfaw,yeap。 – 2012-03-14 23:08:44

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

    } 
+0

$ result2 = $ client-> ShowNameFamely($ params) - > ShowNameFamelyResult; – 2015-04-25 06:12:29

1

我在谷歌上搜索了多参数调用。 所有的线程都没有告诉以下内容。 当PHP调用的.asmx Web服务的参数的流逝必须匹配的Web服务中使用的变量:

public string XYZ(string p, string q) 

Web服务调用必须是这样的:

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

P, q对必须在php调用中进行命名和澄清。