Come invocare un servizio web C # tramite PHP?
-
07-07-2019 - |
Domanda
Ho scritto un servizio web usando ASP.NET (in C #) e sto provando a scrivere un client PHP di esempio usando NuSOAP. Dove sono inciampato su sono esempi di come farlo; alcuni mostrano soapval
in uso (e non capisco bene i parametri - per esempio passando false
come tipi string
, ecc.), mentre altri stanno usando semplicemente array
. Supponiamo che il WSDL per il mio servizio web come riportato da http: // localhost: 3333 / Service.asmx? Wsdl
sia simile a:
POST /Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/webservices/DoSomething"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DoSomething xmlns="http://tempuri.org/webservices">
<anId>int</anId>
<action>string</action>
<parameters>
<Param>
<Value>string</Value>
<Name>string</Name>
</Param>
<Param>
<Value>string</Value>
<Name>string</Name>
</Param>
</parameters>
</DoSomething>
</soap:Body>
</soap:Envelope>
Il mio primo tentativo di PHP è simile a:
<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');
$params = array(
'anId' => 3, //new soapval('anId', 'int', 3),
'action' => 'OMNOMNOMNOM',
'parameters' => array(
'firstName' => 'Scott',
'lastName' => 'Smith'
)
);
$result = $client->call('DoSomething', $params, 'http://tempuri.org/webservices/DoSomething', 'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>
Ora a parte il fatto che il tipo Param è un tipo complesso con il quale sono abbastanza sicuro che il mio semplice tentativo $ array
non funzionerà automaticamente, sto interrompendo il mio servizio web e vedendo il metodo che è stato contrassegnato come WebMethod
(senza rinominarlo, è letteralmente DoSomething
) e vedere gli argomenti sono tutti valori predefiniti ( int
è 0
, la stringa
è null
, ecc.).
Come dovrebbe essere la mia sintassi PHP e cosa devo fare per passare correttamente il tipo Param
?
Soluzione
Devi avvolgere le cose in tonnellate di array nidificati.
<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');
$params = array(
'anId' => 3,
'action' => 'OMNOMNOMNOM',
'parameters' => array(
'Param' => array(
array('Name' => 'firstName', 'Value' => 'Scott'),
array('Name' => 'lastName', 'Value' => 'Smith')
)
)
);
$result = $client->call('DoSomething', array($params),
'http://tempuri.org/webservices/DoSomething',
'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>
Altri suggerimenti
Tipo di non correlato ma dal momento che PHP5 hai il supporto nativo per SOAP.
$client = new SoapClient("some.wsdl"); $client->DoSomething($params);
Potrebbe essere un po 'più conveniente.
Ecco l'esempio con supporto SOAP nativo:
// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient("http://some.wsdl",
array('location' => 'http://127.0.0.100:80/IntegrationService/php'));
$params = array();
$params['lead']['Firstname'] = $user->firstname;
$params['lead']['Lastname'] = $user->lastname;
$params['lead']['Product'] = $product;
$params['lead']['JobTitle'] = $user->job_title;
$params['lead']['Email'] = $user->mail;
$params['lead']['Phone'] = $user->phone;
$params['lead']['CompanyName'] = $user->company_name;
$params['lead']['City'] = $user->city;
$params['lead']['Industry'] = $user->industry;
$client->SubmitLead($params);
Dove '... / IntegrationService / php' nella descrizione di SoapClient è endpoint in WCF:
<endpoint
address="php"
binding="basicHttpBinding"
contract="Integration.Service.IDrupalIntegrationService" />