Di seguito trovate un elenco di esempi utili per la gestione del codice PHP.
Nota: è necessario passare i valori booleani delle opzioni utilizzando gli apici singoli.
Parametri per accedere al web Service
Prima di tutto, è necessario ottenere le seguenti credenziali per accedere al servizio:
- L'indirizzo del web Service (WSDL), ad esempio https://ws-mn1.mag-news.it/ws/wsapi?wsdl
- Un access_token (vedi la documentazione di OAuth 2.0)
Modifica di un contatto
Creare / modificare un contatto.
Se la primaryKey non è nel database sarà creato un nuovo contatto. Se il contatto esiste già, sarà modificato il contatto con la primaryKey specificata.
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'values' => array('field' => 'EMAIL', 'value' => 'name@domain.com'), 'credentials' => $credentials, ); $result = $client->mergeContact($params)->return; if ($result->ok == "1") { //successful action print_r($result); } else { //failed action print_r("ERROR: ".$result->errors->errorType); } } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Iscrizione di un contatto
Creare un nuovo contatto in stato iscritto.
Se un contatto con la stessa primaryKey esiste già, si verificherà un errore. Se devi modificare un contatto già esistente, utilizza la funzione mergeContact.
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'values' => array('field' => "EMAIL", 'value' => 'name@domain.com'), 'credentials' => $credentials, ); $result = $client->subscribeContact($params)->return; if ($result->ok == "1") { print_r($result); } else { print_r("ERROR: ".$result->errors->errorType); } } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Disiscrizione di un contatto
Disiscrivere un contatto.
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'values' => array('field' => "EMAIL", 'value' => 'name@domain.com'), 'credentials' => $credentials, ); $result = $client->unsubscribeContact($params)->return; if ($result->ok == "1") { print_r($result); } else { print_r("ERROR: ".$result->errors->errorType); } } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Modifica del campo password di un contatto
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'values' => array( array('field' => "EMAIL", 'value' => 'name@domain.com'), array('field' => "PASSWORD", 'value' => 'mynewpassword') ), 'credentials' => $credentials, ); $result = $client->mergeContact($params)->return; if ($result->ok == "1") { print_r($result); } else { print_r("ERROR: ".$result->errors->errorType); } } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Modifica del campo Email quando è la chiave primaria (primaryKey)
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'values' => array( array('field' => 'IDCONTACT', 'value' => 'myidcontact'), array('field' => 'EMAIL', 'value' => 'mynewemail@domain.com') ), 'credentials' => $credentials, ); $result = $client->mergeContact($params)->return; if ($result->ok == "1") { print_r($result); } else { print_r("ERROR: ".$result->errors->errorType); } } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Creazione di una comunicazione
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; class content { public $name; public $contentType; public $binaryPayloadEncoding; public $magnewsType; public $binaryPayload; public $text; public $baseUrl; public $tags; public $values; } $content = new content(); $content->contentType = "text/html"; $content->magnewsType = "html"; $content->text = "My content"; $content->name = "MyNewsletterNameAndContentName"; try { $client = new SOAPClient($wsdl_url); $params = array( 'idCampaign' => "myIdCampaign", 'content' => $content, 'options' => array( array('key' => 'EMAIL', 'value' => 'name@domain.com'), array('key' => 'ALIAS_EMAIL', 'value' => 'myNewsletterFrom'), array('key' => 'SUBJECT', 'value' => 'mySubject')), 'credentials' => $credentials, ); $result = $client->createNewsletter($params)->return; print_r($result); } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Spedizione definitiva di una comunicazione
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idNewsletter' => 'myIdNewsletter', 'target' => 'myIdGroup', 'options' => array( 'trial' => false, 'allowEmailDuplicated' => true, 'allowFaxDuplicated' => false, 'allowCellDuplicated' => false, 'enableEmail' => true, 'enableFax' => false, 'enableSms' => false, ), 'advancedOptions' => array( array('key' => 'cssinline', 'value' => 'true'), array('key' => 'webbug', 'value' => 'true')), 'credentials' => $credentials, ); $return = $client->sendNewsletter($params)->return; print_r($return); } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Inviare un messaggio di notifica a un contatto
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; class EmailMessage { public $fromname; public $htmlbody; public $fromemail; public $idmessagetype; public $replyto; public $subject; public $to; } $message = new EmailMessage(); $message->fromname = 'myAlias'; $message->htmlbody = 'myBody and string on which the placeholder value executes the replace'; $message->fromemail = 'mynewemail@domain.com'; $message->replyto = 'myreplytoemail@domain.com'; $message->subject = 'mySubject'; $message->to = "toemail@domain.com"; try { $client = new SOAPClient($wsdl_url); $params = array( 'message' => $message, 'option' => array( array('key'=>'idcontact', 'value'=>1), array('key'=>'iddatabase', 'value'=>1), ),// use instead "body" param, if you want to use newsletter or notification template already present in your account as body of your notification message. You can set options "usenewsletterastemplate=true" and "idnewsletter=xx" where "xx" is the id of the nesletter or notification template. 'credentials' => $credentials, ); $return = $client->sendEmailMessage($params)->return; print_r($return); } catch (Exception $e) { echo "Service not available or generic error occurred: " . $e; }
Cercare un contatto con un ID specificato
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idContact' => "myIdContact", 'credentials' => $credentials, ); $return = $client->findContactById($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Allegare un file a una comunicazione
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idContent' => "myIdContentFile", 'idNewsletter' => "myIdNewsletter", 'format' => "HTML", //or TEXT 'credentials' => $credentials, ); $return = $client->attachFileToNewsletter($params); print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Operazione batch per la modifica di un contatto
Nota: per informazioni sull'opzione deny_subscribed_unsubscribed_contact consulta la documentazione.
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "2", 'values' => array( array('values' => array( array('field' => 'EMAIL', 'value' => 'email1@domain.com'), array('field' => 'NOME', 'value' => 'nameOne'), )), array('values' => array( array('field' => 'EMAIL', 'value' => 'email2@domain.com'), array('field' => 'NOME', 'value' => 'nameTwo'), )) ), 'options' => array('key' => 'deny_subscribed_unsubscribed_contact', 'value' => 'true'), 'credentials' => $credentials, ); $return = $client->batchMergeContacts($params); print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Eseguire una query su un database di contatti
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $paramsQuery = array( 'idDatabase' => "myIdDatabase", 'query' => "SELECT * FROM CONTACTS WHERE email = 'name@domain.com'", 'options' => array('key' => 'only_system_fields', 'value' => 'true'), //visualizza solo i campi di sistema se = true 'credentials' => $credentials, ); $idRowSet = $client->queryContacts($paramsQuery)->return; //ritorna un idRowSet print_r($idRowSet); } catch (Exception $e) { echo "Exception occured: " . $e; } try { $paramsFetch = array( 'idRowSet' => $idRowSet, //l'idRowSet generato precedentemente 'fromIndex' => "0", 'toIndex' => "100", 'credentials' => $credentials, ); $returnFetch = $client->fetchContacts($paramsFetch)->return; print_r($returnFetch); } catch (Exception $e) { echo "Exception occured: " . $e; }
Ottenere i dati statistici di base di una comunicazione
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idNewsletter' => "myIdNewsletter", 'credentials' => $credentials, ); $return = $client->getBasicNewsletterReport($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Creare una lista speciale
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'name' => "mySpecialListName", 'options' => null, 'credentials' => $credentials, ); $return = $client->createSimpleList($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Ottenere le informazioni di base per le audience di un database
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'credentials' => $credentials, ); $return = $client->getGroupsByDatabase($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Ottenere le informazioni di base di un database
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'credentials' => $credentials, ); $return = $client->getBasicDatabaseReport($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Ottenere lo stato di una spedizione
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDeliveryStatus' => "myIdDelivery", 'credentials' => $credentials, ); $return = $client->getDeliveryStatus($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Ottenere le informazioni di base per i campi di un database
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl_url); $params = array( 'idDatabase' => "myIdDatabase", 'credentials' => $credentials, ); $return = $client->getFieldsByDatabase($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Creazione di un file di contenuto (es. un file .pdf)
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } class content { public $name; public $contentType; public $binaryPayloadEncoding; public $magnewsType; public $binaryPayload; public $text; public $baseUrl; public $tags; public $values; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; $filename = "http://support.magnews.it/en/files/MagNews_API_WebServices_en.pdf"; $handle = fopen($filename, "rb"); $contents = ''; $i = 0; while (!feof($handle)) { $contents .= fread($handle, 8192); $i++; } $contentfile = new content(); $contentfile->magnewsType = "file"; $contentfile->contentType = "application/pdf"; $contentfile->name = "myContentName"; $contentfile->binaryPayload = $contents; // content $contentfile->binaryPayloadEncoding = "application/pdf"; // contentType fclose($handle); try { $client = new SOAPClient($wsdl, array('trace' => 1)); $params_create_content = array( 'content' => $contentfile, 'options' => null, 'credentials' => $credentials, ); $idcontenfile = $client->createContent($params_create_content)->return; print_r($idcontenfile); } catch (Exception $e) { echo "Exception occured: " . $e; }
Cerca un contatto con la primaryKey specificata
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $client = new SOAPClient($wsdl, array('trace' => 1)); $params = array( 'idDatabase' => "myIdDatabase", 'primaryKeyValue' => "myPrimaryKey", 'credentials' => $credentials, ); $return = $client->findContactByPrimaryKey($params)->return; print_r($return); } catch (Exception $e) { echo "Exception occured: " . $e; }
Aggiungere informazioni sul contatto a un rowSet e inserire il contatto nel database
class credentials { public $customerId; public $password; public $username; public $type; public $extra; } $credentials = new credentials(); $credentials->password = 'My_Access_Token'; //see OAuth 2 section. $wsdl_url = 'https://ws-mn1.mag-news.it/ws/wsapi?wsdl'; try { $params1 = array( 'credentials' => $credentials, ); $idRowSet = $client->createContactRowSet($params1)->return; print_r($idRowSet); } catch (Exception $e) { echo "Exception occured: " . $e; } try { $params2 = array( 'idRowSet' => "$idRowSet", 'data' => array('values' => array( array('field' => 'EMAIL', 'value' => 'emailtoinsert@domain.com'), array('field' => 'NOME', 'value' => 'nameToInsert'), array('field' => 'IDADDRESSBOOK', 'value' => $idAddressbook), )), 'credentials' => $credentials, ); $update = $client->addDataToContactsRowsSet($params2); print_r($update); } catch (Exception $e) { echo "Exception occured: " . $e; } try { $params = array( 'idRowSet' => $idRowSet, 'options' => array('name' => 'operationName'), 'credentials' => $credentials, ); $idOperation = $client->startBatchContactsUpdate($params); print_r($idOperation); } catch (Exception $e) { echo "Exception occured: " . $e; }