Skip to content

Commit

Permalink
Updated the utility to the latest code.
Browse files Browse the repository at this point in the history
Includes new WSDL and additional features (CallOptions) plus minor bug fixes.
  • Loading branch information
woocash committed Oct 21, 2010
1 parent d13821d commit 1864087
Show file tree
Hide file tree
Showing 3 changed files with 1,695 additions and 30 deletions.
43 changes: 28 additions & 15 deletions call-lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,23 @@ public static function prepDiscreteQuery($body, $fieldStr, $fieldValues) {
} else {
$body .= " and";
}
// Remove duplicates so that multiple records are not returned across discreet queries in the set.
$fieldValues = array_unique($fieldValues);
$querySet = array();
for ($i = 0; $i < count($fieldValues); $i += $MAX_QUERY_WHERE_CLAUSE_COUNT) {
$maxValue = min(count($fieldValues), $i + $MAX_QUERY_WHERE_CLAUSE_COUNT);
$tmpQuery = $body;
$initial = true;
for ($j = $i; $j < $maxValue; $j++) {
$value = $fieldValues[$j];
if (strlen($value) <= 0)
if (strlen($value) <= 0) {
continue;
if ($j > $i) {
}
if (!$initial) {
$tmpQuery .= " or";
}
$tmpQuery .= " " . $fieldStr . "='" . $value . "'";
$initial = false;
}
$querySet[] = $tmpQuery;
}
Expand Down Expand Up @@ -302,8 +307,6 @@ public static function callAPIWithSession($payload, $debug) {
try {
$soapRequest = createRequest(ZuoraAPIHelper::$header->data["session"], $payload);

$soapMethod = getMethod($soapRequest);

if ($debug) {
print "\n\nRequest:\n" . xml_pretty_printer($soapRequest);
}
Expand Down Expand Up @@ -331,6 +334,8 @@ public static function callAPIWithClient($client, $header, $soapRequest, $debug)
$timeBefore = microtime(true);
$result = $client->__soapCall($soapMethod, array(), null, $header);
$timeAfter = microtime(true);

//echo "Request: " . $soapRequest . " Duration: " . ($timeAfter - $timeBefore)/60 . " minutes.\n";

if ($debug) {
print "\nResult:\n" . xml_pretty_printer($client->myResponse);
Expand Down Expand Up @@ -458,7 +463,7 @@ public static function xml_pretty_printer($xml, $html_output=FALSE)

################################################################################
public static function xmlspecialchars($text) {
return str_replace('&#039;', '&apos;', htmlspecialchars($text, ENT_QUOTES, 'UTF-8',false));
return str_replace('&#039;', '&apos;', htmlspecialchars(str_replace('&','&amp;',$text), ENT_QUOTES, 'UTF-8', false));
}

################################################################################
Expand Down Expand Up @@ -535,28 +540,36 @@ public static function createRequest($sessionKey, $payload) {
public static function createRequestWithNS($sessionKey, $payload, $apiNamespace, $objectNamespace) {
global $defaultApiNamespaceURL;
global $defaultObjectNamespaceURL;
return ZuoraAPIHelper::createRequestAndHeadersWithNS($sessionKey, ZuoraAPIHelper::$batchSize, $payload, $apiNamespace, $objectNamespace);
return ZuoraAPIHelper::createRequestAndHeadersWithNS($sessionKey, ZuoraAPIHelper::$batchSize, array(), $payload, $apiNamespace, $objectNamespace);
}

################################################################################
public static function createRequestAndHeadersWithNS($sessionKey, $batchSize, $payload, $apiNamespace, $objectNamespace) {
public static function createRequestAndHeadersWithNS($sessionKey, $batchSize, $callOptions, $payload, $apiNamespace, $objectNamespace) {
global $defaultApiNamespaceURL;
global $defaultObjectNamespaceURL;

if (count(array_keys($callOptions)) > 0) {
$sessionHeader = "<" . $apiNamespace . ":CallOptions>";
foreach ($callOptions as $paramKey => $paramValue) {
$sessionHeader .= "<" . $apiNamespace . ":" . $paramKey . ">" . $paramValue . "</" . $apiNamespace . ":" . $paramKey . ">";
}
$sessionHeader .= "</" . $apiNamespace . ":CallOptions>";
}

$headerParams = array("session"=>$sessionKey);
$sessionHeader = "<" . $objectNamespace . ":SessionHeader>";
$sessionHeader .= "<" . $apiNamespace . ":SessionHeader>";
foreach ($headerParams as $paramKey => $paramValue) {
$sessionHeader .= "<" . $objectNamespace . ":" . $paramKey . ">" . $paramValue . "</" . $objectNamespace . ":" . $paramKey . ">";
$sessionHeader .= "<" . $apiNamespace . ":" . $paramKey . ">" . $paramValue . "</" . $apiNamespace . ":" . $paramKey . ">";
}
$sessionHeader .= "</" . $objectNamespace . ":SessionHeader>";
$sessionHeader .= "</" . $apiNamespace . ":SessionHeader>";

if ($batchSize > 0) {
$headerOptions = array("batchSize"=>$batchSize);
$sessionHeader .= "<" . $objectNamespace . ":QueryOptions>";
$sessionHeader .= "<" . $apiNamespace . ":QueryOptions>";
foreach ($headerOptions as $paramKey => $paramValue) {
$sessionHeader .= "<" . $objectNamespace . ":" . $paramKey . ">" . $paramValue . "</" . $objectNamespace . ":" . $paramKey . ">";
$sessionHeader .= "<" . $apiNamespace . ":" . $paramKey . ">" . $paramValue . "</" . $apiNamespace . ":" . $paramKey . ">";
}
$sessionHeader .= "</" . $objectNamespace . ":QueryOptions>";
$sessionHeader .= "</" . $apiNamespace . ":QueryOptions>";
}

return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:" . $objectNamespace . "=\"" . $defaultObjectNamespaceURL . "\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:" . $apiNamespace . "=\"" . $defaultApiNamespaceURL . "\"><SOAP-ENV:Header>" . $sessionHeader ."</SOAP-ENV:Header><SOAP-ENV:Body>" . $payload . "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
Expand Down Expand Up @@ -698,7 +711,7 @@ public static function getOperationListFromWSDL($wsdl, $debug) {
$node = $xml_obj->xpath("//default:definitions/default:portType/default:operation");
$names = array();
for ($i = 0; $i < count($node); $i++) {
$names[] = $node[$i]->attributes()->name;
$names[] = (string) $node[$i]->attributes()->name;
}
return $names;
}
Expand All @@ -716,7 +729,7 @@ public static function getAPIObjectListFromWSDL($wsdl, $namespace, $debug) {
$node = $xml_obj->xpath("//default:definitions/default:types/xs:schema[@targetNamespace='" . $namespace . "']/xs:complexType");
$names = array();
for ($i = 0; $i < count($node); $i++) {
$names[] = $node[$i]->attributes()->name;
$names[] = (string) $node[$i]->attributes()->name;
}
return $names;
}
Expand Down
169 changes: 154 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
include("call-lib.php");

$DEFAULT_WSDL = 'zuora-19.0-sandbox-AllOptions.wsdl';
$DEFAULT_WSDL = 'zuora-25.0-sandbox-AllOptions.wsdl';
$SUBSCRIBE_TEMPLATE = array(1=>array(
"version"=>'1.0',
"subscribe"=>'subscribe.xml',
Expand Down Expand Up @@ -136,7 +136,7 @@ function dirList ($directory)
// get and use remaining arguments
$username = $_POST['username'];
$password = $_POST['password'];
$body = str_replace("\\\"", "\"", $_SESSION['body']);
$body = $_SESSION['body'];
$wsdl = $_SESSION['wsdl'];

// catch query/api confusion.
Expand All @@ -155,18 +155,23 @@ function dirList ($directory)
}

if ($method == "query") {
$payload = "<ns1:query><ns1:queryString>" . $body . "</ns1:queryString></ns1:query>";
$payload = "<ns1:query><ns1:queryString>" . htmlspecialchars($body) . "</ns1:queryString></ns1:query>";
} else {
$payload = $body;
}

$callOptions = array();
if ($_POST['api-singleTxn']) {
$callOptions = array("useSingleTransaction"=>$_POST['api-singleTxn']);
}

if ($method == "query" || $method == "api") {
try {
$client = createClient($wsdl, $debug);

$header = login($client, $username, $password, $debug);

$soapRequest = ZuoraAPIHelper::createRequestAndHeadersWithNS($header->data["session"], $_SESSION['api-batchSize'], $payload, $_SESSION['api-ns'], $_SESSION['object-ns']);
$soapRequest = ZuoraAPIHelper::createRequestAndHeadersWithNS($header->data["session"], $_SESSION['api-batchSize'], $callOptions, $payload, $_SESSION['api-ns'], $_SESSION['object-ns']);

$timeBefore = microtime(true);
$xml = ZuoraAPIHelper::callAPIWithClient($client, $header, $soapRequest, $debug);
Expand All @@ -191,7 +196,7 @@ function dirList ($directory)
$queryLocator = ZuoraAPIHelper::getQueryLocator($xml);
while ($outputQM && $queryLocator) {
$payload = "<ns1:queryMore><ns1:queryLocator>" . $queryLocator . "</ns1:queryLocator></ns1:queryMore>";
$soapRequest = ZuoraAPIHelper::createRequestAndHeadersWithNS($header->data["session"], $_SESSION['api-batchSize'], $payload, $_SESSION['api-ns'], $_SESSION['object-ns']);
$soapRequest = ZuoraAPIHelper::createRequestAndHeadersWithNS($header->data["session"], $_SESSION['api-batchSize'], $callOptions, $payload, $_SESSION['api-ns'], $_SESSION['object-ns']);

$timeBefore = microtime(true);
$xml = ZuoraAPIHelper::callAPIWithClient($client, $header, $soapRequest, $debug);
Expand Down Expand Up @@ -318,7 +323,20 @@ function dirList ($directory)
<td><input type="submit" value="WSDL" name="wsdl-download"/></td><td><select name="wsdl">
<?php
$wsdl_files = dirList('.');
sort($wsdl_files);
function cmp($a, $b) {
$prefix = '\-';
$av = substrpos($a, $prefix, '.') * -1;
$bv = substrpos($b, $prefix, '.') * -1;
if ($av == $bv) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Reverse sort.
return ($av > $bv) ? -1 : 1;
}
usort($wsdl_files, 'cmp');
foreach ($wsdl_files as $wsdl_filename) {
if (strcasecmp($wsdl_filename,$_SESSION['wsdl']) == 0)
echo "<option value=\"" . $wsdl_filename . "\" selected=\"yes\" >" . $wsdl_filename . "</option>\n";
Expand All @@ -331,31 +349,32 @@ function dirList ($directory)
<tr><td>Password:</td><td><input type="password" size="30" name="password" value=""/></td></tr>
</table>
</td>

<td>&nbsp;</td>
<td>
<table>
<tr><td>API Call Namespaces:</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;<?php echo $defaultApiNamespaceURL ?></td><td>&nbsp;</td><td><input type="text" size="3" name="api-ns" value="<?php echo $_SESSION['api-ns'] ?>"/></td></tr>
<tr><td>&nbsp;<?php echo $defaultObjectNamespaceURL ?></td><td>&nbsp;</td><td><input type="text" size="3" name="object-ns" value="<?php echo $_SESSION['object-ns'] ?>"/></td></tr>
<tr><td>Query Batch Size&nbsp;<input type="text" size="4" name="api-batchSize" value="<?php echo $_SESSION['api-batchSize'] ?>"/></td></tr>
<tr><td>&nbsp;*&nbsp;Default thru v5 100, v6+ 2000.</td></tr>
<tr><td><input type="checkbox" name="api-singleTxn" value="true"/>&nbsp;Call Options: Single Transaction</td></tr>
</table>
</td>

<td>&nbsp;</td>
<td>
<table>
<tr><td>Query Options:</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;Batch Size&nbsp;<input type="text" size="4" name="api-batchSize" value="<?php echo $_SESSION['api-batchSize'] ?>"/></td></tr>
<tr><td>&nbsp;*&nbsp;100 by default, thru v5.<br/>
&nbsp;*&nbsp;2000 by default w/ QueryMore, v6+.</td></tr>
<tr><td>API Call Namespaces:</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;<?php echo $defaultApiNamespaceURL ?></td><td>&nbsp;</td><td><input type="text" size="3" name="api-ns" value="<?php echo $_SESSION['api-ns'] ?>"/></td></tr>
<tr><td>&nbsp;<?php echo $defaultObjectNamespaceURL ?></td><td>&nbsp;</td><td><input type="text" size="3" name="object-ns" value="<?php echo $_SESSION['object-ns'] ?>"/></td></tr>
</table>
</td>

</tr>
</table>

<table>
<tr><td>
Enter your query text or call xml here:<br />
<textarea rows="10" cols="60" name="body" wrap="virtual"><?php echo $_SESSION['body'] ?></textarea><br />
<textarea rows="10" cols="60" name="body" wrap="virtual"><?php echo ZuoraAPIHelper::xmlspecialchars($_SESSION['body']) ?></textarea><br />
<input type="submit" value="Submit" name="submit"/><input type="submit" value="Clear" name="reset"/>
<input type="checkbox" name="csv">CSV Output&nbsp;
<input type="checkbox" name="queryMore">Use QueryMore to get all results<br />
Expand All @@ -378,6 +397,7 @@ function dirList ($directory)
<option value=""></option>
<?php
$names = getObjectListFromWSDL($_SESSION['wsdl'], $debug);
sort($names);
foreach ($names as $name) {
if ($name != "zObject") {
echo "<option value=\"" . $name . "\">" . $name . "</option>\n";
Expand Down Expand Up @@ -414,6 +434,125 @@ function dirList ($directory)
}
}
}
?>

<?php

/** Quoted from http://php.net/manual/en/function.substr.php, submitted by egingell at sisna dot com on 19-Oct-2006 10:19
* string substrpos(string $str, mixed $start [[, mixed $end], boolean $ignore_case])
*
* If $start is a string, substrpos will return the string from the position of the first occuring $start to $end
*
* If $end is a string, substrpos will return the string from $start to the position of the first occuring $end
*
* If the first character in (string) $start or (string) $end is '-', the last occuring string will be used.
*
* If $ignore_case is true, substrpos will not care about the case.
* If $ignore_case is false (or anything that is not (boolean) true, the function will be case sensitive.
* Both of the above: only applies if either $start or $end are strings.
*
* echo substrpos('This is a string with 0123456789 numbers in it.', 5, '5');
* // Prints 'is a string with 01234';
*
* echo substrpos('This is a string with 0123456789 numbers in it.', '5', 5);
* // Prints '56789'
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-string')
* // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', true)
* // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', false)
* // Prints 's is a string with 0123456789 numbers in it and two strings.'
*
* Warnings:
* Since $start and $end both take either a string or an integer:
* If the character or string you are searching $str for is a number, pass it as a quoted string.
* If $end is (integer) 0, an empty string will be returned.
* Since this function takes negative strings ('-search_string'):
* If the string your using in $start or $end is a '-' or begins with a '-' escape it with a '\'.
* This only applies to the *first* character of $start or $end.
*/

// Define stripos() if not defined (PHP < 5).
if (!is_callable("stripos")) {
function stripos($str, $needle, $offset = 0) {
return strpos(strtolower($str), strtolower($needle), $offset);
}
}

function substrpos($str, $start, $end = false, $ignore_case = false) {
// Use variable functions
if ($ignore_case === true) {
$strpos = 'stripos'; // stripos() is included above in case it's not defined (PHP < 5).
} else {
$strpos = 'strpos';
}

// If end is false, set it to the length of $str
if ($end === false) {
$end = strlen($str);
}

// If $start is a string do what's needed to make it an integer position for substr().
if (is_string($start)) {
// If $start begins with '-' start processing until there's no more matches and use the last one found.
if ($start{0} == '-') {
// Strip off the '-'
$start = substr($start, 1);
$found = false;
$pos = 0;
while(($curr_pos = $strpos($str, $start, $pos)) !== false) {
$found = true;
$pos = $curr_pos + 1;
}
if ($found === false) {
$pos = false;
} else {
$pos -= 1;
}
} else {
// If $start begins with '\-', strip off the '\'.
if ($start{0} . $start{1} == '\-') {
$start = substr($start, 1);
}
$pos = $strpos($str, $start);
}
$start = $pos !== false ? $pos : 0;
}

// Chop the string from $start to strlen($str).
$str = substr($str, $start);

// If $end is a string, do exactly what was done to $start, above.
if (is_string($end)) {
if ($end{0} == '-') {
$end = substr($end, 1);
$found = false;
$pos = 0;
while(($curr_pos = strpos($str, $end, $pos)) !== false) {
$found = true;
$pos = $curr_pos + 1;
}
if ($found === false) {
$pos = false;
} else {
$pos -= 1;
}
} else {
if ($end{0} . $end{1} == '\-') {
$end = substr($end, 1);
}
$pos = $strpos($str, $end);
}
$end = $pos !== false ? $pos : strlen($str);
}

// Since $str has already been chopped at $start, we can pass 0 as the new $start for substr()
return substr($str, 0, $end);
}

?>
</form>
</body>
Expand Down
Loading

0 comments on commit 1864087

Please sign in to comment.