[sf 1.4.x] Symfony, Zend et Webservice = crash
Bonjour,
Je post ici car mon problème peut être lié ET à symfony ET aux webservices.
Afin de mettre en application les webservices, j'ai suivi le tuto suivant (à partir du slide n°= 60 )
Exposer des services web SOAP et REST avec symfony 1.4 et Zend Framework@@AMEPARAM@@ssplayer2.swf?doc=rmll-2010-07-06-100707023044-phpapp02&stripped_title=exposer-des-services-web-soap-et-rest-avec-symfony-14-et-zend-framework@@AMEPARAM@@rmll-2010-07-06-100707023044-phpapp02@@AMEPARAM@@exposer-des-services-web-soap-et-rest-avec-symfony-14-et-zend-framework
Quelques explications:
================
* Je tente d'utiliser les types complexes. J'ai donc créé le fichier "/lib/api/ContactType.class.php"
Mes questions:
===========
* Pourquoi mon script "index.php" plante (connexion réinitialisée) uniquement lorsque j'appelle successivement 2 methodes via SoapClient (sachant que la première me crée correctement le contact) ?
* Pourquoi est-ce que je récupère un objet StdClass au lieu de ContactType pour la méthode createContact( ContactType $contact ) dans "ContactService.class.php" ?
* Pour la méthode getContact( $email ) dans "ContactService.class.php", si je spécifie dans la PHPDoc "@return ContactType|null", le WSDL n'en tient pas compte. Et si il n'y a pas de client avec l'email $email, suis-je obligé de renvoyer une exception ? Ne puis-ja pas envoyer un "false" ou "null" (ca me parait plus propre) ?
* Dans "ContactService.class.php" pour la méthode createContact( ContactType $contact ), j'essaye de tirer parti des forumaires Symfony. Est-ce une bonne idée ?
* N'hésitez pas à me faire des remarques, conseils, critiques; je suis pseudo débutant en symfony et jamais créé de webservice
Merci d'avance pour votre patience :-) (oui il y a un gros copier/coller :P )
Modèle de la base de données: Table Contact
===========================================
Code:
1 2 3 4 5 6 7 8 9 10 11
| id bigint(20)
name varchar(255)
email varchar(255)
phone varchar(255)
address varchar(255)
zipcode varchar(255)
city varchar(255)
origin set('formation','sf','elq','chall')
suscribed tinyint(1)
created_at datetime
updated_at datetime |
Coté client
===========
index.php
---------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?php
try
{
$client = new SoapClient( 'http://contact-manager.localhost/api_dev.php/contact/service?wsdl', array(
'trace' => 1,
//'exceptions' => 0,
'cache_wsdl' => WSDL_CACHE_NONE
));
$contact = new stdClass();
$contact->name = 'bloblo';
$contact->email = 'cassecroute@wanadoo.fr';
$contact->phone = '0123456789';
$var = new SoapVar( $contact, XSD_ANYTYPE, 'ContactType' );
$id = $client->createContact( $var );
$response = $client->getContact( 'cassecroute@wanadoo.fr' );
}
catch ( SoapFault $e )
{
exit( '<pre>' . print_r( $e, true ) . '</pre>' );
echo $e->getFile() . '<br/>';
echo $e->getLine() . '<br/>';
exit( 'Exception : ' . $e->getMessage() );
}
?>
<html>
<head></head>
<body>
<?php echo ( isset( $id ) ? '<pre>' . print_r( $id, true ) . '</pre>' : 'Aucun id' ) ?><br/>
<?php echo ( isset( $response ) ? '<pre>' . print_r( $response, true ) . '</pre>' : 'Aucun retour' ) ?>
</body>
</html> |
Coté Serveur
============
/lib/api/ContactType.class.php
------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?php
class ContactType
{
/**
*
* @var int
*/
public $id;
/**
*
* @var string
*/
public $name;
/**
*
* @var string
*/
public $email = '';
/**
*
* @var string
*/
public $phone;
/**
*
* @var string
*/
public $address;
/**
*
* @var string
*/
public $zipcode;
/**
*
* @var string
*/
public $city;
/**
*
* @var string
*/
public $suscribed;
/**
* Construct ContactType from doctrine result
*
* @param Contact $contact
*/
public static function loadDoctrineObject( Contact $contact )
{
$atts = array_keys( get_class_vars( 'ContactType' ) );
$rv = new ContactType();
foreach( $contact as $fieldname => $value )
{
if ( in_array( $fieldname, $atts ) )
$rv->$fieldname = $value;
}
return $rv;
}
}
?> |
/lib/api/ContactService.class.php
---------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php
class ContactService
{
/**
* Return the contact with given email or false if not exists
*
* @param string $email Email du contact
* @return ContactType|null
*/
public function getContact( $email )
{
$rv = null;
$contact = Doctrine::getTable( 'Contact' )->findOneBy( 'email', $email, Doctrine_Core::HYDRATE_ARRAY );
if ( $contact )
{
$contact_type = ContactType::loadDoctrineObject( $contact );
$rv = new SoapVar( $contact_type, SOAP_ENC_OBJECT, 'ContactType' );
}
return $rv;
}
/**
* Create a new contact and return Id
*
* @param ContactType $contact
* @return int
* @throws SoapFault
*/
public function createContact( ContactType $contact )
{
if ( $contact->id )
{
throw new SoapFault( 'ContactService', 'You shouldn\'t fill the field ID in order to create a new contact' );
}
$contact_values = (Array)$contact;
$form = new ApiContactForm();
$form->bind( $contact_values );
if ( !$form->isValid() )
{
$errors = array();
if ( $form->hasGlobalErrors() )
{
foreach( $form->getGlobalErrors() as $error )
{
throw new SoapFault( 'ContactService', (string)$error );
//$errors[] = (string)$error;
}
}
foreach( $form as $field )
{
if ( $field->hasError() )
{
throw new SoapFault( 'ContactService', $field->renderLabelName() . ' : ' . (string)$field->getError() );
//$errors[ $field->getName() ] = (string)$field->getError();
}
}
throw new SoapFault( 'ContactService', 'Unknown error' );
}
try {
$new_contact = $form->save();
}
catch( Exception $e )
{
throw new SoapFault( 'ContactService', $e->getMessage() );
}
return $new_contact->getId();
}
}
?> |