Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: barnabywalters/php-helpers
base: bfa695942d
...
head fork: barnabywalters/php-helpers
compare: a83f5d238a
  • 3 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 102 additions and 1 deletion.
  1. +102 −1 BarnabyWalters/Helpers/Helpers.php
View
103 BarnabyWalters/Helpers/Helpers.php
@@ -4,6 +4,9 @@
use Carbon\Carbon;
use DateTime;
+use DOMDocument;
+use DOMElement;
+use DOMXPath;
/**
* Functional Helpers
@@ -501,7 +504,105 @@ public static function findUrls($text, $tidy = true) {
public static function stringToHexColour($word) {
return substr(bin2hex(metaphone($word, 6)), 0, 6);
}
-
+
+ /**
+ * Parse XML-RPC Method Call
+ *
+ * Given an XML-RPC request body, returns an array like this:
+ *
+ * [
+ * 'methodName': 'example.ex',
+ * 'params': ['string1', 'string2]
+ * ]
+ *
+ * NOTE: Only currently accepts string parameters.
+ *
+ * @todo Make this handle param types other than `string`
+ * @param string $request
+ * @return array
+ */
+ public static function parseXmlRpcMethodCall($request) {
+ $req = new DOMDocument('1.0');
+ $req->loadXML($request);
+
+ $q = new DOMXPath($req);
+ $methodName = $q->query('/methodCall/methodName')->item(0)->nodeValue;
+
+ $params = $q->query('/methodCall/params/param/value/string');
+ $paramArray = [];
+
+ foreach ($params as $node) {
+ $paramArray[] = $node->nodeValue;
+ }
+
+ return [
+ 'methodName' => $methodName,
+ 'params' => $paramArray
+ ];
+ }
+
+ /**
+ * Create XML-RPC Success Response
+ *
+ * Given the value to return (can only be a string at the mo), creates a
+ * response.
+ *
+ * @param string $retVal The string to return
+ * @param bool $asString Whether or not to return the respons as a string or DOMDocument
+ * @return DOMDocument
+ */
+ public static function createXmlRpcSuccessResponse($retVal, $asString = true) {
+ $doc = new DOMDocument('1.0');
+ $methodResponse = new DOMElement('methodResponse');
+ $response->appendChild($methodResponse);
+
+ $string = new DOMElement('string', $retVal);
+ $value = $response->createElement('value');
+ $value->appendChild($string);
+ $param = $response->createElement('param');
+ $param->appendChild($value);
+ $params = $response->createElement('params');
+ $params->appendChild($param);
+ $methodResponse->appendChild($params);
+
+ if (!$asString)
+ return $doc;
+ else
+ return $doc->saveXML();
+ }
+
+ /**
+ * Create XML-RPC Fault Response
+ *
+ * Creates an XML-RPC Fault response with either an int or a string as the
+ * fault.
+ *
+ * @param string|int $fault
+ * @param bool $asString
+ * @return \DOMDocument
+ */
+ public static function createXmlRpcFaultResponse($fault, $asString = true) {
+ if (is_string($fault))
+ $type = 'string';
+ elseif (is_int($fault))
+ $type = 'int';
+
+ $doc = new DOMDocument('1.0');
+ $methodResponse = new DOMElement('methodResponse');
+ $response->appendChild($methodResponse);
+
+ $int = $response->createElement($type, $fault);
+ $value = $response->createElement('value');
+ $value->appendChild($int);
+ $fault = $response->createElement('fault');
+ $fault->appendChild($value);
+ $methodResponse->appendChild($fault);
+
+ if (!$asString)
+ return $doc;
+ else
+ return $doc->saveXML();
+ }
}
// EOF Helpers.php

No commit comments for this range

Something went wrong with that request. Please try again.