Adding / Changing records via php script

I have been thinking about adding records (or altering them) automatically but I need some guidance before I dive in.
It would be based on a third party server sending data to a script on my server from time to time. Either adding, removing or altering data (e.g. new email address or added to a different list).
It would make my life easier before I’m about to send out an email, so that I don’t have to import lists etc.

I know little about MySQL or databases in general so I’ll likely start a little project on freelancer.com or something like that.
But before I do, is there documentation on how to add records to the phplist managed database ?
Or would anybody with some basic knowledge on mysql databases be able to figure that out pretty easily using phpmyadmin, the config.php file etc. ?
And how to set what lists the user/record is subscribed to etc. ?
If there is documentation, kindly point me to it.

Also, is there a small test database I can download somewhere ? I’d rather not have somebody else work on the real deal to try an put together a script that does what I need.

@ibPeter You should look to use the rest api plugin https://resources.phplist.com/plugin/restapi

Thanks. I’m looking into it now.

Unless I’m missing something, the documentation is rather poor though.

For instance right now I’m trying to figure out what the url should be for the callApi() function.

EDIT1:

I now see this in the comments:
"The API cannot handle Subscriber Attributes at the moment"
That’s actually a big let down as I need to set a few fields. So I’ll end up importing manually afterwards anyway.
( I suppose I got a bit too excited when I installed this plugin )

EDIT2:

The API is very promising but then ends up to be disappointing. Once I finally figured out what url to use and that I had to call login before calling another function, and once I got all that to work properly, the next function ‘subscriberAdd’ failed and due to poor and conflicting docs I have no idea why. As far as I can tell I’m doing everything right. I’m about to abandon this.

If you have another suggestion as to how to access the database and its properties, I’d be happy to know.

I got things to work after all. A good night’s sleep to get rid of the frustration.

I’ll probably use it but not being able to set values to custom fields is a problem still.

Despite initial frustration (my bad) I’ve got it working at it’s working well. So thanks for the tip.

I’m now also looking into adding some custom code to change user attributes.
It only works half though, I’ve ran into a road block but I have posted a question in the developer section.
Hopefully I get an answer there so that I can complete the function.
FYI: https://discuss.phplist.org/t/set-change-user-attributes-via-rest-api-almost-fully-working

1 Like

Peter,

Could you share what you did to make this work? I have been using the PHPList application for a few years to send emails out to a list of people but I am now trying to integrate the subscription of users from my site’s welcome page to PHPList. I want the people to just add their email address in a text field on my site and click “submit” which would call the API from there to register the user. I have installed the API and it is enabled and working (from what PHPList is telling me). I have downloaded the code from https://resources.phplist.com/plugin/restapi as we are told but nothing is happening. I am using the url specified in the API page in PHPList and I am using the name and password of the account I use weekly. I don’t know what else I am doing wrong as this does not seem to connect or work. Nothing happens. I am at a complete loss.

Thanks

Dave

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

/***********************************************************/

Thank you. I will try to work with what you gave me and let you know what happens.

Thanks again.

Dave

I will try to work with what you gave me and let you know what happens.

Sounds like a plan Dave.

Following this thread. Note that 3.4.1 released the API, and I have enabled it. But wondering if there are any code changes that you needed to do to get your class to work with 3.4.1? (Initial look of API docs show them to be a bit sparse, but still digging into them.)

If so, can you post the updated code?

Thanks…Rick…

I think the API referred to earlier in this thread is the old one which is a plugin for phpList 3, and officially depreciated. The one referred to in recent releases (phpList 3.4.x) uses a different code base and is incompatible with the above code snippets.

For any documentation that is incorrect or inadequate, please either higher the issues so they can be addressed by others or submit improvements directly (the manual is just a public Git repo, and the wiki has open registration).

Thanks!