Sandip's Programming Zen

An attempt to share tech/coding experiences

Calling Asp.Net web service from PHP

with 20 comments

I just had a scenario where one of our client needs to consume our Asp.Net web service in their PHP host application.

Calling Asp.Net web service from PHP turned out to be easier then I thought. Here is how you can do.

First you need to download open source nusoap soap library, and put the lib folder under your application root directory. (I have renamed the folder name lib to nusoap in app directory).

The sample PHP code is below:

<?php
require_once(‘nusoap/nusoap.php’);

$client = new nusoap_client(‘http://www.yourdomain.com/service.asmx?WSDL&#8217;, ‘wsdl’,”, ”, ”, ”);

$err = $client->getError();
if ($err) {
echo ‘<h2>Constructor error</h2><pre>’ . $err . ‘</pre>’;
}
$param = ”;
$result = $client->call(‘functionName’, $param, ”, ”, false, true);

if ($client->fault) {
echo ‘<h2>Fault</h2><pre>’;
print_r($result);
echo ‘</pre>’;
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo ‘<h2>Error</h2><pre>’ . $err . ‘</pre>’;
} else {
// Display the result
echo ‘<h2>Result</h2><pre>’;
print_r($result);
echo ‘</pre>’;
}
}
?>

Of course, you can do much more than this, check out samples provided in downloaded nusoap source.

Advertisements

Written by Sandip

April 4, 2009 at 11:06 am

Posted in .Net, PHP, Programming

Tagged with ,

20 Responses

Subscribe to comments with RSS.

  1. thanks

    Jeff

    April 30, 2009 at 12:53 am

  2. thanks for this.

    James Radford

    October 13, 2009 at 2:59 pm

  3. I am using your sample code and getting blank page. i am using asp.net web service and for php using apache web service please reply soon

    thanks

    shaz

    January 31, 2010 at 6:17 pm

    • Normally if there is a response then it might not show on the web browser, you have to check using “view source” menu option, its simply because response is not in html format.
      Sorry for the late reply.

      Sandip

      February 12, 2010 at 9:22 am

  4. Can you help, how can i generate such request using nusoap.

    POST /vrm.asmx HTTP/1.1
    Host: http://www.vrm.etgws.co.uk
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: “EurotaxGlass’s/WebService/VehicleLookup/VRM/Search”

    string
    string
    string
    boolean
    string

    string

    Mazhar

    February 11, 2010 at 8:23 pm

    • Just check the documentation, they have enough samples created.

      Sandip

      February 12, 2010 at 9:21 am

  5. hi friends
    i am facing one issue: i’m calling a ASP.NET web service using above code, but not able to pass string parameter to the service. instead a null value is passed to the service.

    Thanks in advance.

    soudeep

    April 2, 2010 at 5:24 pm

    • Put your code here and I will see what is going wrong.

      Sandip

      April 3, 2010 at 8:52 am

  6. Hi, do you know how to create web service in object oriented php? I mean the server has methods on classes.

    rayyan

    April 5, 2010 at 6:10 am

  7. No, nusoap is using SOAP protocol and the example above it RPC protocol. both are different but uses same medium (xml, http).

    Here is more…
    http://weblog.masukomi.org/writings/xml-rpc_vs_soap.htm

    Sandip

    April 6, 2010 at 3:49 am

    • okey.
      but how to create object oriented web service using php and nusoap?
      i have searched in google but no result.

      Thanks for the kind answers.

      rayyan

      April 6, 2010 at 4:17 am

  8. You are right seems nothing on google, I think you have to get your hands dirty and write your own, first try with a “hello world” app and build upon it.

    Good luck.

    Sandip

    April 6, 2010 at 2:54 pm

  9. hi
    i want to access ASP.Net Webservese function “login” and i m using this same code but getting some error

    require_once(‘nusoap-0.9.5/lib/nusoap.php’);
    $client = new nusoap_client(‘http://isixtech.com/projects/SmallWeb/Empower2BytePro.asmx?WSDL’, ‘wsdl’,”, “, “, “);

    $err = $client->getError();
    if ($err) {
    echo ‘Constructor error

    ' . $err . '

    ‘;
    }
    $param = “”;
    $result = $client->call(‘Login’, $parameters, “, “, false, true);

    if ($client->fault) {
    echo ‘Fault

    ';
    print_r($result);
    echo '

    ‘;
    } else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
    // Display the error
    echo ‘Error

    ' . $err . '

    ‘;
    } else {
    // Display the result
    echo ‘Result

    ';
    print_r($result);
    echo '

    ‘;
    }
    }

    /////////////// Error is ////////////////////////////
    wsdl error: Getting http://isixtech.com/projects/SmallWeb/Empower2BytePro.asmx?WSDL – HTTP ERROR: Couldn’t open socket connection to server http://isixtech.com/projects/SmallWeb/Empower2BytePro.asmx?WSDL prior to connect(). This is often a problem looking up the host name.

    plz reply soon

    Imran Durrani

    May 19, 2011 at 1:22 pm

  10. hi
    i used your script.but i get only function values (e.g hello).i am passing ‘abc’ .so result must ‘hello abc’
    but i get only hello.my parameter is not reach to function.why?

    monika

    July 7, 2011 at 5:15 pm

    • Hi Monika,

      It looks like you are only getting object but not it’s value, to get the value from the object you have to serialize it or covert it to appropriate datatype. For example if “my” is array then try to fetch value as hello.my[0] .

      Hope this helps.

      Sandip

      July 8, 2011 at 10:53 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: