Page 1 of 1

Php curl and aweber

Posted: Sun Oct 10, 2010 12:08 am
by kaleshwarchand
Hi all,
I need to make a script that will log into aweber and add some followup messages,
I can get it to login and go all the way to the message form, but when submitting the form I get "Error Your session has expired, please log in again." from aweber. any help highly appreciated.

bit that gives error (full code is below)

Code: Select all

//submit the form
    //this is the part that gives the error
    curl_setopt( $ch, CURLOPT_URL, 'https://www.aweber.com/users/followups/add' );
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);  
full code

Code: Select all

<?php
 error_reporting(E_ALL);
 ini_set("display_errors", 1); 
set_time_limit(0);
if(!isset($_POST["dirName"]))
{
    show_form();
    exit();
}

aweber_login();

for($a=1;$a<2;$a++)
{
    echo('doing '.$a. '<br/>');
    $data=array();
    $data=get_data($a);
    $data['data[FreqMesg][FreqMesg][mesg_num]']=$a;
    if($a>1)
    {
        $data['&data[FreqMesg][FreqMesg][mesg_freq]']=7;
    }
    
    //go to followups page
    $ch = curl_init('https://www.aweber.com/users/followups');
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_exec($ch);    
    
    //simulate click on add followup link
    curl_setopt( $ch, CURLOPT_URL, 'https://www.aweber.com/users/followups/add' );
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_exec($ch);
    
    //submit the form
    //this is the part that gives the error
    curl_setopt( $ch, CURLOPT_URL, 'https://www.aweber.com/users/followups/add' );
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);    
    echo('done '.$a. '<br/>');
    
}

function aweber_login()
{
    $ch = curl_init('https://www.aweber.com/login.htm');
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_exec($ch);
    $ch = curl_init('https://www.aweber.com/login.htm');
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "user and pass data");
    curl_exec($ch);
    //exit();
}

function show_form()
{
    echo('
    <form method="post" >
    dir name<input type="text" name="dirName"><br>
    file name<input type="text" name="fileName"><br>
    AFF1<input type="text" name="Aff1"><br>
    AFF2<input type="text" name="Aff2"><br>
    AFF3<input type="text" name="Aff3"><br>
    AFF4<input type="text" name="Aff4"><br>
    AFF5<input type="text" name="Aff5"><br>
    <input type="submit" value="submit"></form>');
}

function get_data($num)
{
$filename=$_POST["dirName"].'/'.$_POST["fileName"].$num.'.txt';
    if(is_file($filename))
    {
    $content=file_get_contents($filename);
    $con=explode("\n",$content,2);
    //print_r($con);
    //exit();
    $subject=str_replace('Subject:','',$con[0]);
    $subject=str_replace('%%FIRSTNAME%%','{!firstname}',$subject);
    $htmlcontent=nl2br($con[1]);
    $htmlcontent=str_replace('%%FIRSTNAME%%','{!firstname}',$htmlcontent);
    $htmlcontent=str_replace('%%YOURWEBSITE%%','<a hre="http://www.homebizops.net/">http://www.homebizops.net/</a>',$htmlcontent);
    $htmlcontent=str_replace('%%YOURNAME%%','Max Profit',$htmlcontent);
    $htmlcontent=str_replace('%%YOURADDRESS%%','P.O. Box 909, Newburyport, MA 01950 USA',$htmlcontent);
    $htmlcontent=str_replace('%%AFFILIATELINK1%%','<a href="'.$_POST["Aff1"].'">'.$_POST["Aff1"].'</a>',$htmlcontent);
    $htmlcontent=str_replace('%%AFFILIATELINK2%%','<a href="'.$_POST["Aff2"].'">'.$_POST["Aff2"].'</a>',$htmlcontent);
    $htmlcontent=str_replace('%%AFFILIATELINK3%%','<a href="'.$_POST["Aff3"].'">'.$_POST["Aff3"].'</a>',$htmlcontent);
    $htmlcontent=str_replace('%%AFFILIATELINK4%%','<a href="'.$_POST["Aff4"].'">'.$_POST["Aff4"].'</a>',$htmlcontent);
    $htmlcontent=str_replace('%%AFFILIATELINK5%%','<a href="'.$_POST["Aff5"].'">'.$_POST["Aff5"].'</a>',$htmlcontent);
    
    //$content=str_replace('%%FIRSTNAME%%','{!firstname}',$con[0]);
    $content=str_replace('%%YOURWEBSITE%%','<a hre="http://www.homebizops.net/">http://www.homebizops.net/</a>',$con[1]);
    $content=str_replace('%%YOURNAME%%','Max Profit',$content);
    $content=str_replace('%%YOURADDRESS%%','P.O. Box 909, Newburyport, MA 01950 USA',$content);
    $content=str_replace('%%AFFILIATELINK1%%',$_POST["Aff1"],$content);
    $content=str_replace('%%AFFILIATELINK2%%',$_POST["Aff2"],$content);
    $content=str_replace('%%AFFILIATELINK3%%',$_POST["Aff3"],$content);
    $content=str_replace('%%AFFILIATELINK4%%',$_POST["Aff4"],$content);
    $content=str_replace('%%AFFILIATELINK5%%',$_POST["Aff5"],$content);    
    $data=array();
    $data['subject']=$subject;
    $data['oldversion']=0;
    $data['html_body2']='';
    $data['html_body']=$htmlcontent;
    $data['body']=$content;
    $data['data[FreqMesg][FreqMesg][track_click_rate]']=1;
    $data['data[submit]']=1;
    $data['data[checkspam]']=0;
    return($data);
    }
}

?>