如何从php调用我的asp.net web服务

本文关键字:asp net web 服务 我的 调用 php | 更新日期: 2023-09-27 17:58:43

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

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

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 web服务的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端需要更改?。。。请引导我摆脱这个问题。。。

如何从php调用我的asp.net web服务

这段代码对我来说很好…

<?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);
 ?> 

试试这个。

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

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

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

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

HTH