openx XMLRPC api示例

// 1. 设置环境
if (!@include('xmlrpc/XML/RPC.php')) {
          die('Error: cannot load the PEAR XML_RPC class');
}

$xmlRpcHost = 'localhost';
$xmlRpcPort = '8080';
$webXmlRpcDir = '/www/api/v1/xmlrpc';
$username = 'myname';
$password = 'ssss';

$logonXmlRpcWebServiceUrl  = $webXmlRpcDir . '/LogonXmlRpcService.php';

/// 登录且返回sessionID
$aParams = array(
        new XML_RPC_Value($username, 'string'),
        new XML_RPC_Value($password, 'string')
);
$oMessage  = new XML_RPC_Message('logon', $aParams);
$oClient   = new XML_RPC_Client($logonXmlRpcWebServiceUrl, $xmlRpcHost, $xmlRpcPort);
$oResponse = $oClient->send($oMessage);
if (!$oResponse) {
    die('Communication error: ' . $oClient->errstr);
}
$sessionId = returnXmlRpcResponseData($oResponse);

$i=1;
foreach( get_agency_list() as $r1 ) {
    $agency_id   = $r1['agencyId'];
    $agency_name = $r1['agencyName'];

    foreach( get_publisher_list_by_agency_id($agency_id) as $r2) {
        $publisher_id     = $r2['publisherId'];
        $publisher_name   = $r2['publisherName'];

        foreach (  get_zone_list_by_publisher_id($publisher_id) as $r3 ) {
            $zone_id    = $r3['zoneId'];
            $zone_name  = $r3['zoneName'];

            /*
            $r4 =  get_code_by_zoneid($zone_id);
            echo "===$agency_id,$publisher_id,$zone_id===\n";
            echo $r4,"\n\n";
            */

            $str = "            %d => array(
                    'domain' => 'admin1',
                    'zoneid' => %d,
                    'zonenm' => \"%s\",
                    'adcode' => \"\"
            ),\n";
            printf($str, $i++, $zone_id, $zone_name);

        }
    }
}


//登录注销
$aParams   = array(new XML_RPC_Value($sessionId, 'string'));
$oMessage  = new XML_RPC_Message('logoff', $aParams);
$oClient   = new XML_RPC_Client($logonXmlRpcWebServiceUrl, $xmlRpcHost, $xmlRpcPort);
$oResponse = $oClient->send($oMessage);


function returnXmlRpcResponseData($oResponse) {
    if (!$oResponse->faultCode()) {
        $oVal = $oResponse->value();
        $data = XML_RPC_decode($oVal);
        return $data;
    } else {
        die('Fault Code: ' . $oResponse->faultCode() . "\n" .
                'Fault Reason: ' . $oResponse->faultString() . "\n");
    }
}

function get_agency_list() {

    /// 取得所有Agency (list)
    global $webXmlRpcDir;
    global $xmlRpcHost;
    global $xmlRpcPort;
    global $sessionId;

    $adverurl = $webXmlRpcDir . '/AgencyXmlRpcService.php';
    $aParams    = array(
            new XML_RPC_Value($sessionId, 'string'),
            );
    $oMessage   = new XML_RPC_Message('getAgencyList', $aParams);
    $oClient    = new XML_RPC_Client($adverurl, $xmlRpcHost, $xmlRpcPort);
    $oResponse  = $oClient->send($oMessage);
    $result     = returnXmlRpcResponseData($oResponse);
    return $result;

}

function get_publisher_list_by_agency_id($agency_id) {

    /// 取得一个Agency下所有publisher (list)
    global $webXmlRpcDir;
    global $xmlRpcHost;
    global $xmlRpcPort;
    global $sessionId;

    $adverurl = $webXmlRpcDir . '/PublisherXmlRpcService.php';
    $aParams    = array(
            new XML_RPC_Value($sessionId, 'string'),
            new XML_RPC_Value($agency_id,'int'),
            );
    $oMessage   = new XML_RPC_Message('getPublisherListByAgencyId', $aParams);
    $oClient    = new XML_RPC_Client($adverurl, $xmlRpcHost, $xmlRpcPort);
    $oResponse  = $oClient->send($oMessage);
    $result     = returnXmlRpcResponseData($oResponse);
    return $result;
}

function get_advertiser_list_by_agency_id($agency_id) {

    /// 取得一个Agency下所有广告主 (list)
    global $webXmlRpcDir;
    global $xmlRpcHost;
    global $xmlRpcPort;
    global $sessionId;

    $adverurl = $webXmlRpcDir . '/AdvertiserXmlRpcService.php';
    $aParams    = array(
            new XML_RPC_Value($sessionId, 'string'),
            new XML_RPC_Value($agency_id,'int'),
            );
    $oMessage   = new XML_RPC_Message('getAdvertiserListByAgencyId', $aParams);
    $oClient    = new XML_RPC_Client($adverurl, $xmlRpcHost, $xmlRpcPort);
    $oResponse  = $oClient->send($oMessage);
    $result     = returnXmlRpcResponseData($oResponse);
    return $result;
}

function get_zone_list_by_publisher_id($pub_id) {
    /// 取得一个特定广告主的所有zone (list)
    global $webXmlRpcDir;
    global $xmlRpcHost;
    global $xmlRpcPort;
    global $sessionId;

    $bannerurl = $webXmlRpcDir . '/ZoneXmlRpcService.php';
    $aParams    = array(
            new XML_RPC_Value($sessionId, 'string'),
            new XML_RPC_Value($pub_id,'int'),
            );
    $oMessage   = new XML_RPC_Message('getZoneListByPublisherId', $aParams);
    $oClient    = new XML_RPC_Client($bannerurl, $xmlRpcHost, $xmlRpcPort);
    $oResponse  = $oClient->send($oMessage);
    $result     = returnXmlRpcResponseData($oResponse);
    return $result;
}


function get_code_by_zoneid($zone_id) {
    /// 取得一个特定zone 所生成的code
    global $webXmlRpcDir;
    global $xmlRpcHost;
    global $xmlRpcPort;
    global $sessionId;

    $zoneurl = $webXmlRpcDir . '/ZoneXmlRpcService.php';
    $lastParams = new XML_RPC_Value(
            array(
                'target'    => new XML_RPC_Value('_blank', 'string'),
                'source'    => new XML_RPC_Value('', 'string'),
                'refresh'   => new XML_RPC_Value('', 'string'),
                'resize'   => new XML_RPC_Value(0, 'int'),
                'transparent'     => new XML_RPC_Value( 0, 'int'),
                'ilayer'          => new XML_RPC_Value( 0, 'int'),
                'iframetracking'  => new XML_RPC_Value( 1, 'int'),
                'thirdpartytrack' => new XML_RPC_Value( 0, 'int'),
                'cachebuster'     => new XML_RPC_Value( 1, 'int'),
                'comments'        => new XML_RPC_Value( 0, 'int'),
                ),
            'struct'
            );
    $aParams    = array(
            new XML_RPC_Value($sessionId, 'string'),
            new XML_RPC_Value($zone_id,'int'),
            new XML_RPC_Value("adframe",'string'),
            $lastParams
            );
    $oMessage   = new XML_RPC_Message('generateTags', $aParams);
    $oClient    = new XML_RPC_Client($zoneurl, $xmlRpcHost, $xmlRpcPort);
    $oResponse  = $oClient->send($oMessage);
    $result     = returnXmlRpcResponseData($oResponse);

    return $result;
}

你可能感兴趣的:(xml,api,list,function,String,Comments)