【问题标题】:how to call my asp.net web service from php?如何从 php 调用我的 asp.net web 服务?
【发布时间】:2013-01-26 11:53:16
【问题描述】:

我在 asp.net 中创建了一个 Web 服务,并在 iis 5.1 中发布。现在我想从 php 环境中调用这个 Web 服务。实际上我的 web 服务获取一个字符串作为参数并返回相同的字符串。但是一直返回的字符串为空或 null。我无法将字符串值从 php 发送到 asp.net web 服务...

这是我在 asp.net 中创建的网络服务

namespace PRS_WS
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 prs_point : System.Web.Services.WebService
    {
        [WebMethod]
        public string testAssignment(string inputData)
        {
            return inputData;           
        }
    }
}

而且,这是我调用上述 asp.net 网络服务的 php 代码...

<?php
       require_once('nusoap/lib/nusoap.php');
       $wsdl="http://localhost/prs_point/prs_point.asmx?WSDL";
       $str1="";
       $str1="Hello from php";

        $client = new soapclient($wsdl,'wsdl');
        $result=$client->call('testAssignment',$str1);

         foreach($result as $key => $value)
        {
              echo "<br/>Response ::::: $value";
         }
?>  

不知道是php端还是asp.net端需要修改?...请指导我摆脱这个问题...

【问题讨论】:

    标签: c# php asp.net .net web-services


    【解决方案1】:

    这段代码对我来说很好......

    <?php
    
    require 'nusoap/lib/nusoap.php';
    $client = new nusoap_client('http://localhost/prs_point/prs_point.asmx?WSDL', 'WSDL');
    
    
    $error = $client->getError();
    if ($error) {
        die("client construction error: {$error}\n");
    }
    
    $param = array('inputData' => 'sample data');
    $answer = $client->call('testAssignment', array('parameters' => $param), '', '', false, true);
    
    $error = $client->getError();
    if ($error) {
        print_r($client->response);
        print_r($client->getDebug());
        die();
     }
    
     print_r($answer);
    
     ?> 
    

    【讨论】:

    • 谢谢你,萨拉瓦南。
    【解决方案2】:

    您最好在 WCF 中定义您的服务,这可以让您更好地控制生成的 SOAP,虽然我不确定 PHP,但我在过去让 ASMX Web 服务与 Adob​​e Flex 一起工作时遇到过问题因此,尽管它仍然使用 SOAP 协议,但集成并不总是无缝的。除此之外,您的服务看起来不错,但我认为您需要以下几行:

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    

    在 PHP 方面,您应该可以调用 $result = $client -&gt; testAssignment( $str1 ); 但(我忘记了)您可能需要访问结果值 $result = $client -&gt; testAssignment( $str1 ) -&gt; testAssignmentResult; 您还必须将参数传递给绑定在数组中的方法,而不是调用具有多个参数,请参阅this article 以获取完整示例。

    HTH

    【讨论】:

      【解决方案3】:

      试试这个。

      $client = new SoapClient("http://localhost/prs_point/prs_point.asmx?WSDL");
      $params->inputData= 'Hello';
      
      $result = $client->testAssignment($params)->testAssignmentResult;
      
      echo $result;
      

      【讨论】:

      • 我试过你的代码...但它抛出以下错误...致命错误:调用 C:\wamp\www\soap\tets.php 中未定义的方法soapclient::testAssignment()在第 11 行
      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多