Skip to content

Instantly share code, notes, and snippets.

@brendanc
Created March 19, 2013 15:37
Show Gist options
  • Save brendanc/5197124 to your computer and use it in GitHub Desktop.
Save brendanc/5197124 to your computer and use it in GitHub Desktop.
<?php
/* Here is some generic error handling code, not related to actual sample */
set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');
function my_exception_handler($e) {
exit('Houston! We have a problem: <br />'.$e);
}
function my_error_handler($no,$str,$file,$line) {
$e = new ErrorException($str,$no,0,$file,$line);
my_exception_handler($e); /* Do not throw, simply call error handler with exception object */
}
/* end of error handling code */
//swap out with your Litmus account info here
class Credentials { public $subdomain = "***"; public $user = "***"; public $password = "***";}
function litmus_get_request($url){
$credentials = new Credentials();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://".$credentials->subdomain.".litmus.com/".$url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials->user.":".$credentials->password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml","Accept: application/xml"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec($ch); // send the request to Litmus
return $result;
}
function litmus_post_request($url,$post_data){
$credentials = new Credentials();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://".$credentials->subdomain.".litmus.com/".$url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials->user.":".$credentials->password);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml","Accept: application/xml", "Content-length: ".strlen($post_data)));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec($ch); // send the request to Litmus
return $result;
}
function get_litmus_email_clients(){
$client_xml = litmus_get_request("emails/clients.xml");
$clients = array();
//using SimpleXML to parse results, see http://www.php.net/manual/en/simplexml.examples-basic.php
$testing_applications = new SimpleXMLElement($client_xml);
foreach ($testing_applications->testing_application as $app) {
//here we're only using email clients, we are excluding spam clients
if($app->result_type == 'email') {
array_push($clients,(string) $app->application_code);
}
}
return $clients;
}
function create_litmus_email_test($clients){
$client_array = '';
foreach($clients as $client){
$client_array = $client_array."<application><code>".$client."</code></application>";
}
$post_data = "<?xml version=\"1.0\"?><test_set>
<applications type=\"array\">
".$client_array."
</applications>
<save_defaults>false</save_defaults>
<use_defaults>false</use_defaults>
</test_set>";
$test_xml = litmus_post_request('emails.xml',$post_data);
//using SimpleXML to parse results, see http://www.php.net/manual/en/simplexml.examples-basic.php
$test_set = new SimpleXMLElement($test_xml);
return (string)$test_set->test_set_versions[0]->test_set_version->url_or_guid;
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//create our Litmus test and get the inbox_guid
$clients = get_litmus_email_clients();
$guid = create_litmus_email_test($clients);
//a simple POST - Redirect - GET pattern
header("Location: ".$_SERVER['REQUEST_URI']."?g=".$guid);
exit();
}
else{
?>
<html>
<head>
<title>Litmus Customer Api Demo</title>
</head>
<body>
<?php
if(isset($_GET['g'])){
echo "You've successfully created a Litmus test. Please send your email to test to <a href='mailto:".$_GET['g']."'>".$_GET['g']."</a>";
}
else {
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>">
<input type="submit" value="Create"></input>
</form>
<?php } ?>
</body>
</html>
<?php
}
?>
@brendanc
Copy link
Author

A not that awesome example of how you could create a Litmus test using the customer api and PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment