Last active
October 13, 2015 15:38
-
-
Save richardbenson/4217894 to your computer and use it in GitHub Desktop.
ifttt to movabletype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Variables, set these | |
$xmlrpc_fullurl = "http://www.example.com/cgi-bin/mt/mt-xmlrpc.cgi"; // path to your mt-xmlrpc.cgi file | |
$blog_id = "3"; //The blog ID to post to, as IFTTT has no options for this we force it. | |
//Fetch the request and turn it into an XML element for editing | |
$request_body = file_get_contents('php://input'); | |
$methodCall = new SimpleXMLElement($request_body); | |
//Check if the title includes a blog id in the form "title {x}" | |
$title = $methodCall->params->param[3]->value->struct->member[0]->value->string; | |
if (preg_match('/.*\{([0-9]+)\}/', $title, $matches)) { | |
$blog_id = $matches[1]; | |
$title = preg_replace('/\{([0-9]+)\}/', '', $title); | |
$methodCall->params->param[3]->value->struct->member[0]->value->string = trim($title); | |
} | |
//Sometimes ifttt will insert the right variable type, sometimes it won't so remove | |
//entirely and replace with what we need instead | |
$methodCall->params->param[0]->value = ""; | |
$methodCall->params->param[0]->value->addChild('i4', $blog_id); | |
//POST this onto the mt implementation of xmlrpc | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $xmlrpc_fullurl ); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt($ch, CURLOPT_POST, 1 ); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $methodCall->asXML() ); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); | |
$result = curl_exec($ch); | |
//Send back everything to ifttt | |
$length = strlen($result); | |
header('Connection: close'); | |
header('Content-Length: '.$length); | |
header('Content-Type: text/xml'); | |
header('Date: '.date('r')); | |
echo $result; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment