Need beginner help with using the API

I’ve installed & configured PHPList, got it working, and installed the RESTAPI plugin. I’m looking through the sample client file and the unit test file, and I don’t know what to do next. Do I upload the sample client file to my server and include/require it in the files that will use it? Or is the file that declares the class I need already there? I don’t normally work with classes, so the terminology is stumping me.

Also, where do I set the URL, login name, etc. - right in the client file, by changing
private $url;
to
private $url = "https://website.com/lists/admin/?pi=restapi&page=call"
where "website’ is my domain? The unit test file does this very differently, relying on constants, but I don’t understand where it gets those constants or if I can do the same thing - I’d like to, to avoid repeating the URL and login name in multiple places.

Thanks for any help getting me over this “how to get started using the API” hump!
Holly

Still struggling with this, hoping for a smidgin of help! Using the sample client file that defines the class phpListRESTApiClient, I uploaded this file to the server. Then I made a little test file that includes that file and has the following code:

$url = ‘https://durhamrecordsonline.com/lists/admin/?pi=restapi&page=call’;
$loginName = ‘xxx’; //where xxx is the correct loginName
$password = ‘yyy’; // and yyy is the correct password
$api = new phpListRESTApiClient($url, $loginName, $password, $secret = ‘’);
$result = $api->login();
echo "
after login: "; var_dump($result);

and I get:

after login: bool(false)

‘login’ uses callAPI. I have verified, via echo and var_dump, that the post_params array received by callAPI has the right values. When I var_dump the $result in callAPI, after the CURL execution, it is NULL - it’s supposed to have the CURL string that was executed. I know nothing about CURL. I added

    if (curl_errno($c)) {
        print "Error: ".curl_error($c).PHP_EOL;
    }

to the callAPI routine and it does not show a CURL error. I don’t know where to go with this.

Please, if anybody has successfully written code that uses the current API, please show me the lines in your file where you make your initial calls to the API. I’m sure I’m missing something really basic.

You should enable error reporting

error_reporting(-1);

also, have you confirmed that the curl extension is installed?

@hollycochran I tried the client and after a bit of fiddling around managed to get it working. A couple of things that I had to change

  • ensure that the temp directory is set to somewhere that is writeable
  • if the admin name or password are incorrect then phplist returns the html of the login page, not a structured response. This makes the json_decode fail.

include_once 'phpListRESTApiClient.php';

error_reporting(-1);
$apiURL = 'https://zzzz/lists/admin/?pi=restapi&page=call';
$login = 'xxx';
$password = 'yyy';

$phpList = new phpListRESTApiClient($apiURL, $login, $password);


if ($phpList->login()) {
    echo 'logged in ';
} else {
    echo 'login failed';
}

Duncan - thanks for helping! CURL is installed and working; I use it to talk to Paypal every day. My /tmp dir is at the same level as my public_html dir, and I am running my test file in the public_html dir, so I set public $tmpPath = …/tmp’;
and set the permissions for the tmp dir to 777. I also tried making the path to tmp absolute: /home/myusername/tmp
which made no difference. For what it’s worth, the cookie file does not appear in the tmp dir.
The username and password were copied from the config file, and I can log into the PHPList Admin area with them, so they must be correct. BUT…I put a var_dump of $result after the curl_exec in callAPI, and indeed it’s returning the HTML of the login page AND it includes this text:

restapi : call
Dashboard
incorrect password

Turning on error reporting confirmed this, saying ‘Trying to get property of non-object’ for this line of the login function:
return $result->status == ‘success’;

I changed my password to one without any special chars, in case there was some sort of URL encoding problem, and that made no difference. I added:
$info = curl_getinfo($c);
and var_dumped $info - it’s an array that includes the URL and a bunch of times, but it doesn’t show me the POSTFIELDS - I was hoping to see if it was messing up the password somehow before it passes it to CURL.

Do you have any other suggestions for debugging this?

You seem to be referring to the database user name and password which is in the config file. To use the restapi you need to use a phplist admin id and password, such as the one that you use to login to phplist itself.

AAAAAHH! I knew it was going to be something very basic. I thought I was supposed to use the database username and password because (in my mind), the API was communicating with the database - but no, it’s communicating with the code itself. Makes perfect sense now. I changed it to the Admin username and password and I can now log in via the API.
Thank you so much, Duncan!