2008-11-10 10 views
3

J'ai écrit un service web en utilisant ASP.NET (en C#) et j'essaye d'écrire un exemple de client PHP en utilisant NuSOAP. Où je suis tombé dessus sont des exemples de comment faire cela; certains montrent soapval étant utilisé (et je ne comprends pas bien les paramètres - par exemple en passant false comme string types, etc.), tandis que d'autres utilisent simplement array s. Disons que le WSDL pour mon service Web tel que rapporté par http://localhost:3333/Service.asmx?wsdl ressemble à quelque chose comme:Comment appeler un service web C# via PHP?

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> 

Ma première tentative de PHP ressemble:

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

maintenant à part le type Param étant un type complexe que je » Je suis assez sûr que mon simple $array tentative ne fonctionnera pas automatiquement, je suis point d'arrêt dans mon service web et voir la méthode que j'ai marqué comme WebMethod (sans le renommer, c'est littéralement DoSomething) et voir les arguments sont toutes les valeurs par défaut (le int est 0, le string est null, etc.).

À quoi devrait ressembler ma syntaxe PHP, et que dois-je faire pour passer le type Param correctement?

Répondre

6

Vous devez emballer des objets dans des tonnes de tableaux imbriqués.

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

Merci, J'ai pris une aversion pour PHP après cette expérience. – cfeduke

+1

Vraiment? Vous blâmez PHP pour une bibliothèque tierce? C'est comme prendre une aversion pour la crème glacée après avoir mangé mauvais caramel au beurre. – Anthony

3

Sorte de non lié mais depuis PHP5 vous avez un support natif pour SOAP.

 
$client = new SoapClient("some.wsdl"); 
$client->DoSomething($params); 

Cela pourrait être un peu plus pratique.

http://se.php.net/soap

1

Voici l'échantillon avec le support de SOAP natif:

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

Où .../IntegrationService/php 'dans la description SoapClient est point final dans WCF:

<endpoint 
      address="php" 
      binding="basicHttpBinding" 
      contract="Integration.Service.IDrupalIntegrationService" /> 
Questions connexes