OpenX API的调用

这两天需要从openx广告系统中获取指定广告的点击量, 将统计数据生成折线图和饼图, 于是乎就去查看了下openx的调用方法, 刚开始并不是很顺利, 连接都没有成功建立. 网上有v1和v2两个api的版本, 第一想法肯定是选择最新的版本.
参考的内容有:
OpenxAPI http://developer.openx.org/api/
OpenxExample https://developer.openx.org/wiki/display/API/Home

基本测试代码如下:
<?php
$config = array(
    'host' => 'localhost',
    'www_path' => '/openx/www',
    'username' => 'admin',
    'password' => '******',
    'file' => '/var/www/openx',
    'xmlRpcDir' => '/openx/www/api/v2/xmlrpc/'
);

$service_url = $config['xmlRpcDir'];

if (!function_exists('xml_rpc_se')) {
    include($config['file'] . '/lib/pear/XML/RPC.php');
}

$debug = true;

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");
    }   
}

$oClient = new XML_RPC_Client($service_url, $config['host']);
$oClient->setdebug($debug);

// Logon
$aParams = array(
    new XML_RPC_Value($config['username'], 'string'), 
    new XML_RPC_Value($config['password'], 'string')
);
$oMessage = new XML_RPC_Message('ox.logon', $aParams);
$oResponse = $oClient->send($oMessage);
if (!$oResponse) {
    die('Communication error: ' . $oClient->errstr);

$sessionId = returnXmlRpcResponseData($oResponse);
echo '*** User logged on with session Id : ' . $sessionId . "<br/>\n";

// Get an advertiser
$aParams = array(
    new XML_RPC_Value($sessionId, 'string'),
    new XML_RPC_Value(1, 'int')
);
$oMessage = new XML_RPC_Message('ox.getAdvertiser', $aParams);
$oResponse = $oClient->send($oMessage);
print_r(returnXmlRpcResponseData($oResponse));

echo "<br/>\n";

// Logoff
$aParams = array(new XML_RPC_Value($sessionId, 'string'));
$oMessage = new XML_RPC_Message('ox.logoff', $aParams);
$oResponse = $oClient->send($oMessage);
echo "*** User with session Id : $sessionId logged off <br/>\n";

你可能感兴趣的:(xml,PHP)