2012-03-14 83 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調用中進行命名和澄清。