Home  >  Article  >  Backend Development  >  PHP calls webservice written in c# .NET (passed in personal test)_PHP tutorial

PHP calls webservice written in c# .NET (passed in personal test)_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:07:001009browse

First the results picture——
PHP calls webservice written in c# .NET (passed in personal test)_PHP tutorial

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
///


///Summary description of ibmfashion
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

// To allow this web service to be called from a script using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ibmfashion : System.Web.Services.WebService {
Public ibmfashion () {
​​​​ //If using designed components, please uncomment the following line
​​​​ //InitializeComponent();
}
[WebMethod]
Public string HelloWorld() {
         return "Hello World";
}
[WebMethod]
Public int multiplication(int a, int b)
{
         return a*b;
}
}

PHP calls c# webservice code:
// Pull in the NuSOAP code
ob_start();
require_once('lib/nusoap.php');

$url ="http://localhost:8787/wcf/ibmfashion.asmx?wsdl";
$client = new nusoap_client($url, 'wsdl','','','','');
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';
//Parameters are converted to arrays and passed
$ary = array('a' => 11, 'b' => 22);
$result = $client->call('multiplication',$ary);
echo "

".print_r($result,true)."
";

//Error and debug information
if ($client->fault) {
echo '

Fault

';<br>
print_r($result);<br>
echo '
';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '

Error

' . $err . '
';
} else {
// Display the result
echo '

Result

';<br>
print_r($result);<br>
echo '
';
}
}
// Display the debug messages
echo '

Debug

';
echo '
' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '
';


?>

Summarize several commonly used methods for calling c# .NET webservice from php:
Method 1:
Check whether there is php_soap.dll in the System32 directory. If there is no online download, put it in this directory.

Find the configuration file php.ini file and open the following extension
extension = php_soap.dll
extension = php_curl.dll
extension = php_openssl.dll
The PHP calling code is as follows:
Method 1:
$url="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);

$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8'

$result = $client->__soapCall("UserLogin",array("UserLogin"=>array(
'str' => '{"userName":"3","password":"222"}')));
if (is_soap_fault($result)) {
​ trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}

Method 2:
Also use php_soap.dll, but the code is slightly different:

$url="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);

$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';

$result = $client->UserLogin(array('str' => '{"userName":"3","password":"222"}'));

if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}
Method 3:
NuSoap is a WebService programming tool in the PHP environment, used to create or call WebService. It is an open source software, which is a series of PHP classes written entirely in PHP language that sends and receives SOAP messages through HTTP. It is developed by NuSphere Corporation (http://dietrich.ganx4.com/nusoap/). One advantage of NuSOAP is that it does not require extension library support. This feature allows NuSoap to be used in all PHP environments and is not affected by server security settings.

During the processing process, be sure to pay attention to whether the parameters provided by WebService match and are correct.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477901.htmlTechArticleFirst, the result chart C# code: using System; using System.Collections.Generic; using System.Linq; using System .Web; using System.Web.Services; using System.Web.Services.Protocols; //...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn