Find herewith a list of examples useful to manage PHP Code.
Note: you need to pass the options boolean values surrounded by single quotes.
Parameters to login to Web Service
First of all, you need to get the following credentials to login to the service:
- Web Service address (WSDL), for example https://ws-mn1.mag-news.it/ws/wsapi?wsdl
- An access_token (see OAuth 2.0 documentation)
Modification of a contact
Creates/updates a contact. If the primaryKey is not in the database, it creates a new contact. If the contacts already exists, it updates the contact with the primaryKey specified.
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; }
Subscription of a contact
Creates a new contact in active status. If a contact with the same primaryKey already exists, then an error occurs. If you need to update an existing contact, it is recommended to use the function 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; }
Unsubscription of a contact
Unsubscribes a contact.
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; }
Modification of the contact's password field
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; }
Modification of the field Email when it is the 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; }
Communication creation
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; }
Final delivery of a communication
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; }
Sending a notification message to a contact
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; }
Search for a contact with the id specified
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; }
Attach a file to a communication
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; }
Batch operation for a contact modification
Note: for information about the option deny_subscribed_unsubscribed_contact please consult the documentation.
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; }
Perform a query on a database of contacts
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; }
Get basic statistical data of a communication
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; }
Create a special list
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; }
Get basic information for audiences of a 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; }
Get basic information of a 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; }
Get a delivery status
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; }
Get basic information for fields of a 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; }
Creation of a content file (e.g. a .pdf file)
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; }
Search for a contact with the specified 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, 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; }
Add contact info to a rowSet and insert contact to 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; }