Five9 的 API:如何使用 SOAP API 和基本身份验证提取报告

Posted

技术标签:

【中文标题】Five9 的 API:如何使用 SOAP API 和基本身份验证提取报告【英文标题】:Five9's API: How to pull reports using SOAP API and Basic Authentication 【发布时间】:2012-11-12 17:28:52 【问题描述】:

我们正在尝试使用报告 API 从 Five9 的服务器访问数据。我们在下面编写了代码,但没有得到任何结果。对我来说,问题似乎与对 Five9 服务器的身份验证有关。请检查帮助我们了解如何定期提取特定活动的数据并将其存储在数据仓库中。

<?php
$soapUser = "USERNAME";  //  username
$soapPassword = "DEMOPASSWORD"; // password

$soap_options   = array( 'login' => $soapUser, 'password' => $soapPassword );
$auth_details   = base64_encode($soapUser.":".$soapPassword);

$client = new SoapClient("https://api.five9.com/wsadmin/v2/AdminWebService?wsdl",       $soap_options);
$header = new SoapHeader("https://api.five9.com/wsadmin/v2/AdminWebService/getCallLogReport", "authentication", "Basic $auth_details"); 
//echo "Response:\n" . $client->__getLastResponse() . "\n";
$client->__setSoapHeaders($header);

$xml_data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://service.admin.ws.five9.com/v2/AdminWebService/getCallLogReport">
<soapenv:Header/>
<soapenv:Body>
  <v2:getCallLogReport>
        <campaigns>Campaign1</campaigns>
  </v2:getCallLogReport>
</soapenv:Body>
</soapenv:Envelope>';

echo $result = $client->getCallLogReport($xml_data,   "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl", "https://api.five9.com/wsadmin/v2/AdminWebService/getCallLogReport",0); 


?>  

示例 XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:v2="http://service.admin.ws.five9.com/v2/">
<soapenv:Header/>
<soapenv:Body>
  <v2:getCallLogReport>
     <!--Optional:-->
     <time>
        <!--Optional:-->
        <end>?</end>
        <!--Optional:-->
        <start>?</start>
     </time>
     <!--Optional:-->
     <criteria>
        <!--Optional:-->
        <ANI>?</ANI>
        <!--Zero or more repetitions:-->
        <agents>?</agents>
        <!--Zero or more repetitions:-->
        <callTypes>?</callTypes>
        <!--Zero or more repetitions:-->
        <campaigns>?</campaigns>
        <!--Optional:-->
        <DNIS>?</DNIS>
        <!--Zero or more repetitions:-->
        <dispositions>?</dispositions>
        <!--Zero or more repetitions:-->
        <lists>?</lists>
        <!--Zero or more repetitions:-->
        <skillGroups>?</skillGroups>
     </criteria>
  </v2:getCallLogReport>
  </soapenv:Body>
 </soapenv:Envelope>

【问题讨论】:

【参考方案1】:

看起来您的问题是您在soap标头中发送了base64编码的用户名/密码。它实际上需要包含在 http 标头中。我的解决方案是 ruby​​,但希望它可以帮助你。

soap_client = Savon.client(
  endpoint: "https://api.five9.com/wsadmin/AdminWebService/", 
  namespace: "http://service.admin.ws.five9.com/", 
  headers:  "Authorization" => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" , 
  env_namespace: :soapenv, 
  namespace_identifier: :ser, 
  ssl_verify_mode: :none
)

message =  
  "lookupCriteria" => 
"criteria" => 
      "field" => "email_address",
      "value" => "something@example.com"
    
  


response = soap_client.call(:getContactRecords, message: message)
p response.body

所以你的 XML 最终看起来像这样。

SOAP request: https://api.five9.com/wsadmin/AdminWebService/
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==, SOAPAction: "getContactRecords",
Content-Type: text/xml;charset=UTF-8, Content-Length: 471

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ser="http://service.admin.ws.five9.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ser:getContactRecords>
      <lookupCriteria>
        <criteria>
          <field>email_address</field>
          <value>something@example.com</value>
        </criteria>
      </lookupCriteria>
    </ser:getContactRecords>
  </soapenv:Body>
</soapenv:Envelope>

HTTPI POST request to api.five9.com (httpclient)
SOAP response (status 200)

<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
  <env:Header></env:Header>
  <env:Body>
    <ns2:getContactRecordsResponse xmlns:ns2="http://service.admin.ws.five9.com/" xmlns:ns3="http://service.admin.ws.five9.com/v1/">
      <return>
        <fields>number1</fields>
        <fields>email_address</fields>
        <records>
          <values>
            <data>5555555555</data>
            <data>something@example.com</data>
          </values>
        </records>
      </return>
    </ns2:getContactRecordsResponse>
  </env:Body>
</env:Envelope>

【讨论】:

【参考方案2】:

我知道这是一个老问题,但我们最近改用 Five9,但我找不到任何 PHP 示例可以使用。下面说明如何使用标准凭据进行连接,并执行呼叫列表。我已经包含了整个选择标准结构(已注释掉)供您参考。如果包含选择属性,则必须指定相应的条件。

$soap = null;
$wsdl = "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl";

$user = "yourloginid";
$pass = "yourpassword";

$soap_options = array("login" => $user, "password" => $pass);

$soap = new SoapClient($wsdl, $soap_options);

/* create the callLogReportCriteria data selection structure */
$arryParams['time']  = array("start" => "2013-05-05T00:00:01",
                             "end" => "2013-05-05T09:00:00");
$arryParams['criteria'] = array("callTypes" =>  array("INBOUND","OUTBOUND"));

/************ Entire Structure for selection criteria *************/
/*$arryParams['criteria'] = array("ANI" =>  "6178752803",
                                "Agents" => "",
                                "callTypes" =>  array("INBOUND","OUTBOUND"),
                                "campaigns" =>  "",
                                "dispositions" => "",
                                "Lists" =>  "",
                                "skillGroups" => ""
                               );*/

$result = $soap->getCallLogReport($arryParams);

if(isset($result->return->records)) 
    /* you have records returned */
    $objRecords = $result->return->records;

    for($i=0 ; $i < sizeof($objRecords) ; $i++) 
        /* do your processing */
        printf("ANI: %s<br />", $objRecords[$i]->values->data[3]); //4th element has ANI
    


某些代码行可以合并,但为了便于理解,我将它们分开。您还需要在实际的 SOAP 调用周围使用 try/catch 来进行错误处理。

希望这将有助于缩短某人的学习曲线。我知道我很想在一个月前拥有这个!

【讨论】:

【参考方案3】:

@JesseQ 很好的例子!帮了我很多。以下是我如何设置它以在他们的武器库中运行任何 Five9 报告,包括您自己创建的报告。对于自定义报告,您需要创建一个新文件夹/报告并将其放置在 Five9 门户网站的“自定义报告”部分。希望这会有所帮助。

MySQL 数据库连接 (dbConnect.php)

<?php
    $mysqli = new mysqli("your db's IP address", "your db user", "your db password", "your db");
    if ($mysqli->connect_error) 
        die('Connect Error: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    
?>

Five9/MySQL 数据库导入

<?php
    require_once("../include/dbConnect.php");
    $startDate = new DateTime();
    $endDate = new DateTime();
    $startDate->setDate(2015, 07, 22);
    $endDate->setDate(2015, 07, 22);
    $five9 = "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl";
    $five9Credentials = array("login" => "your Five9 username", "password" => "your Five9 password");
    $soap = new SoapClient($five9, $five9Credentials);
    $runReportParam["criteria"]["time"] = array("start" => $startDate->format("Y-m-d\T00:00:00"), "end" => $endDate->format("Y-m-d\T23:59:59"));
    $runReportParam["folderName"] = "My Custom Reports";
    $runReportParam["reportName"] = "My First Report";
    $runReportResult = $soap->runReport($runReportParam);
    if(isset($runReportResult->return))
        $runReportData = $runReportResult->return;
        $isReportRunningParam["identifier"] = $runReportData;
        $isReportRunningParam["timeout"] = 10;
        $isReportRunningResult = $soap->isReportRunning($isReportRunningParam);
        if(empty($isReportRunningResult->return))
            $getReportResultParam["identifier"] = $runReportData;
            $getReportResult = $soap->getReportResult($getReportResultParam);
            if(isset($getReportResult->return->records))                
                $getReportResultData = $getReportResult->return->records;
                echo "[" . date("Y-m-d h:i:s") . "] " . $runReportData . "\n";
                for($x = 0; $x < $xx = count($getReportResultData); $x++)
                    $query = "REPLACE INTO MyTable(
                        CallDate, CallTime, DNIS, Disposition, Zip, AreaCode, ANI)
                        VALUES (?,?,?,?,?,?,?)";
                    $result = $mysqli->prepare($query);
                    $result->bind_param("sssssss", 
                        $getReportResultData[$x]->values->data[0],
                        $getReportResultData[$x]->values->data[1],
                        $getReportResultData[$x]->values->data[2],
                        $getReportResultData[$x]->values->data[3],
                        $getReportResultData[$x]->values->data[4],
                        $getReportResultData[$x]->values->data[5],
                        $getReportResultData[$x]->values->data[6]
                    );
                    $result->execute();
                    $result->store_result();
                    if ($result->error)
                        die('Connect Error: (' . $result->errno . ') ' . $result->error);
                    
                    echo "[" . date("Y-m-d h:i:s") . "] " . $x . "\n";
                
             else 
                echo "Error: " . $runReportData . " returned no data";
            
         else 
            echo "Error: " . $runReportData . " exceeded the report runtime limit";
        
     else 
        echo "Error: " . $runReportParam["reportName"] . " wasn't found";
    
    $mysqli->close();
?>

【讨论】:

以上是关于Five9 的 API:如何使用 SOAP API 和基本身份验证提取报告的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JAVA 中使用 UsernameToken 创建 SOAP API 请求?

在构建 SOAP API 时如何使用 SSL?

如何同时使用 SOAP WCF 服务和 REST API

如何使用 c# 为 paypal CreateInvoice api 创建 SOAP XML 请求消息

如何在不使用dotnet中的安全令牌(使用soap api)的情况下登录salesforce?

在python中调用没有WSDL的soap API