Hi Dave,
It’s been a while and I’m already rusty on the specifics. I know I needed some time (as well) to get it to work. Certainly if you don’t do these things every day the learning curve is a bit steeper.
But I ended up making a class (I think cfr the examples) of which I make an instance when I need it.
In my case a remote server POSTs something to a php page and when it does I instantiate the class and I call the relevant functions that I implemented.
If I understand you correctly what you’re doing is basically the same. Only difference is that the POST comes from one of your own pages. So in the php page where you handle / process the POSTed data, you can then make an instance of the class and call the functions you want to call. You’ll have to write that class and implement the functionality that makes sense to you of course. Basically a wrapper around the existing API functions.
Here’s my (stripped down) constructor and Login() function (which is called during construction). I noticed Login does not always succeed immediately, so I call it a few times with a delay in between (when deemed necessary).
/***********************************************************/
public function __construct()
{
$cnt = 0 ;
while ($cnt < 3)
{
$result = $this->Login() ;
if ($result->status != 'success')
{
sleep($cnt + 1) ; // Delay
$cnt++ ;
}
else $cnt = 3 ;
}
if ($result->status != 'success')
{
$this->ReportStatus('Could not log on to phplist') ;
}
}
/***********************************************************/
public function Login()
{
$post_params = array(
'login' => 'your_login_here',
'password' => 'your_password_here',
);
$result = $this->callAPI('login', $post_params);
$this->LogStatus("Login()", $result) ;
return $result;
}
/***********************************************************/
I hope this helps ?
EDIT, you’ll also need this then:
/***********************************************************/
private function callAPI($command, $post_params, $no_decode = false)
{
$post_params['cmd'] = $command;
$post_params['secret'] = 'your_secret';
$post_params = http_build_query($post_params);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'https://www.your_website.com/your_phplist_folder/admin/?page=call&pi=restapi');
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($c, CURLOPT_COOKIEFILE, '/var/tmp/phpList_RESTAPI_Helper_cookiejar.txt');
curl_setopt($c, CURLOPT_COOKIEJAR, '/var/tmp/phpList_RESTAPI_Helper_cookiejar.txt');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Connection: Keep-Alive', 'Keep-Alive: 60'));
$result = curl_exec($c);
if (!$no_decode) {
$result = json_decode($result);
}
return $result;
}
/***********************************************************/