Home / General / How to fetch email status and other details from aweber through aweber API?

How to fetch email status and other details from aweber through aweber API?

Below are the complete code to fetch the aweber account subscriber status and other details through aweber API

Download AWeber API PHP Library

Select Code
<?php
function httpPost($url,$params)
{
  $postData = '';
   //create name value pairs seperated by &
   foreach($params as $k => $v)
   {
      $postData .= $k . '='.$v.'&';
   }
   rtrim($postData, '&');
    $ch = curl_init();  
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    
    $output=curl_exec($ch); 
    curl_close($ch);    
    return $output;
}

function getOauthVerifier($result){
    preg_matthVerifier($result){
    preg_match('/<a href="(.+)">/', $result, $match); 
    $info = parse_url($match[1]);
    foreach (explode('&', str_replace('amp;','',$info['query'])) as $couple) {
        list ($key, $val) = explode('=', $couple);
        $arr[$key] = $val;
    }
    return $arr['oauth_verifier'];
}

require_once('aweber_api/aweber_api.php');
$consumerKey    = 'xxxxxxxxxxxxxxxxxxxxxxxx'; # put your credentials here
$consumerSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; # put your credentials here
$aweber = new AWeberAPI($consumerKey, $consumerSecret);

$aweber->adapter->debug = false;
# set this to true to view the actual api request and response

if (empty($_COOKIE['accessToken'])) {
    
    if (empty($_GET['oauth_token'])) {
        $callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        list($requestToken, $requestTokenSecret) = $aweber->getRequestToken($callbackUrl);
        setcookie('requestTokenSecret', $requestTokenSecret);
        setcookie('callbackUrl', $callbackUrl);     

        $data['oauth_username']='xxxxxxxxxx';# put your aweber login username here
        $data['oauth_password']='xxxxxxxx';# # put your aweber login password here
        $data['oauth_submit']='Allow Access';
        $data['oauth_callback']=$callbackUrl;
        $data['display']='page';
        $data['oauth_token']=$requestToken;
        $result = httpPost($aweber->getAuthorizeUrl(), $data);
        $oauth_verifier=getOauthVerifier($result);
        header("Location: ".$callbackUrl."?"."oauth_token=".$requestToken."&oauth_verifier=".$oauth_verifier); 
        exit();
    }

    $aweber->user->tokenSecret = $_COOKIE['requestTokenSecret'];
    $aweber->user->requestToken = $_GET['oauth_token'];
    $aweber->user->verifier = $_GET['oauth_verifier'];
    list($accessToken, $accessTokenSecret) = $aweber->getAccessToken();
    setcookie('accessToken', $accessToken);
    setcookie('accessTokenSecret', $accessTokenSecret);
    header('Location: '.$_COOKIE['callbackUrl']);
    exit();
}

$account = $aweber->getAccount($_COOKIE['accessToken'], 
$_COOKIE['accessTokenSecret']);
$AWeberCollection=$account->findSubscribers(array('email' => 'subscriber_email_id@domainname.com'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>AWeber Application</title>
   <style>
table{
    width:100%;
}
td{
    border:1px solid #ccc;
    min-width:50px;
     
    overflow:auto;
}
h1{
 font-size:16px;color:#f10;
}
h2{
 font-size:14px;color:#f10;
}
</style>
<body>
 
<?php    
$k=1;
echo "<table>";
foreach($AWeberCollection->data['entries'] as $arrVal){
    if($k==1){
        echo "<tr>";
        echo "<td>S.No.</td>";
        foreach($arrVal as $key=>$val){
            echo "<td>&nbsp;".$key."</td>";
        }
        echo "</tr>";    
    }
    echo "<tr>";
    echo "<td>&nbsp;".$k."</td>";
    foreach($arrVal as $key=>$val){
        echo "<td>&nbsp;".$val."</td>";
    }
    echo "</tr>";        
    $k++;
}
echo "</table>";
?>
<body>
</html>

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.