WHMCS getclients搜索结果

时间:2014-01-20 17:25:53

标签: php xml search multidimensional-array whmcs

我和一些朋友正在尝试使用WHMCS在虚拟世界中提供服务。问题是WHMCS没有提供一种简单的方法来搜索特定的客户端记录而没有客户端ID,这些客户端ID不会存储在WHMCS数据库之外的任何地方。 api函数getclients以XML格式返回结果,问题是当您使用此方法搜索客户端时,您只能搜索名字,姓氏或电子邮件地址。现在我们尝试传递firstname和lastname的变量(它们必须单独传递)这不幸地返回X firstname或Y Lastname的所有客户端的客户端记录,而不是用X和Y缩小一个客户端。

我想知道的是如何搜索从XML结果生成的PHP数组,以尝试仅为我们正在寻找的客户端获取客户端记录。

结果如下:

数组([WHMCSAPI] =>数组([ACTION] => getclients [RESULT] =>成功[TOTALRESULTS] => 2 [STARTNUMBER] => 0 [NUMRETURNED] => 2 [客户] =>数组([CLIENT] =>数组([ID] => 9 [FIRSTNAME] => Test1 [LASTNAME] => Test2 [COMPANYNAME] => [EMAIL] => test1 @ test .com [DATECREATED] => 2013-04-24 [GROUPID] => 1 [STATUS] =>有效)[CLIENT1] =>数组([ID] => 20 [FIRSTNAME] => Test3 [LASTNAME] => Test2 [COMPANYNAME] => [EMAIL] => test@test.com [DATECREATED] => 2014-01-20 [GROUPID] => 0 [状态] =>有效) )))

我们尝试搜索的代码是:

$postfields["action"] = "getclients";
$postfields["search"] = $firstname;
$postfields["search"] = $lastname;
$postfields["responsetype"] = "xml";

 $query_string = "";
 foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 $xml = curl_exec($ch);
 if (curl_error($ch) || !$xml) $xml = '<whmcsapi><result>error</result>'.
 '<message>Connection Error</message><curlerror>'.
 curl_errno($ch).' - '.curl_error($ch).'</curlerror></whmcsapi>';
 curl_close($ch);

 $arr = whmcsapi_xml_parser($xml); # Parse XML
 $client = searchClient($firstname, $lastname, $arr);

 print_r($client); # Output XML Response as Array

 /*
 Debug Output - Uncomment if needed to troubleshoot problems
 echo "<textarea rows=50 cols=100>Request: ".print_r($postfields,true);
 echo "\nResponse: ".htmlentities($xml)."\n\nArray: ".print_r($arr,true);
 echo "</textarea>";
 */

 function whmcsapi_xml_parser($rawxml) {
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $rawxml, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
$alreadyused = array();
$x=0;
foreach ($vals as $xml_elem) {
  if ($xml_elem['type'] == 'open') {
     if (in_array($xml_elem['tag'],$alreadyused)) {
        $x++;
        $xml_elem['tag'] = $xml_elem['tag'].$x;
     }
     $level[$xml_elem['level']] = $xml_elem['tag'];
     $alreadyused[] = $xml_elem['tag'];
  }
  if ($xml_elem['type'] == 'complete') {
   $start_level = 1;
   $php_stmt = '$params';
   while($start_level < $xml_elem['level']) {
     $php_stmt .= '[$level['.$start_level.']]';
     $start_level++;
   }
   $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
   @eval($php_stmt);
  }
}
return($params);
 }
 function searchClient($first, $last, $array)
 {
     foreach ($array as $key => $val)
 {
     if($val['FIRSTNAME'] == $first && $val['LASTNAME'] == $last)
     {
         return $key;
     }
 }
 return null;
 }
 ?>

返回空白结果。我承认我并不完全确定如何做到这一点所以任何指针都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用他们的api通过电子邮件地址“搜索特定的客户记录”,他们现在支持响应类型的json格式

“请注意 需要clientid或电子邮件。”

http://docs.whmcs.com/API:Get_Clients_Details