back to phpList.org

From blank PHP page to interacting with the API? How do you call the functions?

Hey everyone. I’ve never used an API with bare-bone documentation. While all the available functions are well documented, the actual implementation is not documented at all. I’ve not used APIs enough to fill in the blanks. What needs to be included, what needs to be required, how do you call the API functions via PHP?

This is my starting point, I’m using APIv1 due to it supporting campaign creation via php.

<?php 

    echo('api test');

    require('../phpList/admin/plugins/restapi.php');

    function testCreateCampaign($templateId) {

        $post_params = array(

            'subject' => 'Test Campaign created by API '.time(),

            'fromfield' => 'From Name apitest@phplist.com',

            'replyto' => '',

            'message' => 'Test Message',

            'textmessage' => 'Text',

            'footer' => 'Footer',

            'status' => 'submitted',

            'sendformat' => 'both',

            'template' => $templateId,

            'embargo' => date('Y-m-d'),

            'rsstemplate' => '',

            'owner' => 0,

            'htmlformatted' => 1,

        );

        

        $result = $this->callAPI('campaignAdd', $post_params);

        $this->assertEquals('success', $result->status);

        $campaignID = $result->data->id;

        return $campaignID;

    }

?>

$templateId

By the looks of it the $templateId is dependent on another functions that is supposed to return the template. Do I include that as well or do I call the function for hetting the template first? The example doc “api-calls-available.html” says that template is supposed to be a string. Does that mean I can just type in the template name, thus not needing the dependent function?

PS: I don’t have an api folder in the phplist/ installed directory.

Can I pay somebody here to get some help with this? And I mean get educated on how to use this, not talking about having someone do this for me.

@MilesThatch You are probably going to need to look at the php code of the plugin to see exactly how each method and its parameters work.

Also, create a campaign through phplist to see the data that you need to enter. Then look at the plugin’s methods to find which need to be called.

Regarding your first question about template field, that is the template id which is an integer not a string. The plugin documentation is wrong in this respect. But if you are creating the campaign content automatically then you might want to create the complete campaign HTML document and not use a template at all. In that case just set the template to 0.

I’ve been indeed rooting around the plugin and documentation to figured out how I can make my php script communicated with the platform. The problem is from unclarity of how to even make the API plugin become a part of our script. There’s lack of setup documentation to make sense how the connection is made.

If you take a look at the documentation for the phpMailer. The github page contains an example script that actually includes the info on what needs to be included / required in the code page to make your PHP script actually reach the phpmailer tool. I haven not found anything like that in the phplist docs. The platform assumes that you should already know that.

The plugin page https://resources.phplist.com/plugin/restapi refers to an example client class. You can use that class in your own php code to make requests.

I’m not sure why you are looking at phpmailer. That is used by phplist to send out emails and is unrelated to the plugin.

I’ve mentioned php mailer as an example of what a clear to understand documentation looks like. When I was experimenting with php mailer, took me under 10 minutes to get everything up and running. Phplist plugin skips a large chunk of setup as its supposedly targeted towards more veteral developers who know the inbetween steps. What are those steps, post installation?

Take a look at my code in the first post. That code is straight from the documentation. It doesnt work. Why? I don’t know. There are no errors on the page, no error logs on the server. I have no idea why it doesn’t work. Possibly because I didnt set up the connection between my script and the plugin correctly because there’s no documentation on how to do that - just a functions list.

Look at the example client class that I mentioned to see how to use the plugin.

In the first post you have copied code from a phpunit test. That is an example only of setting up the parameters and calling the API, you are not going to be running php unit tests. The example client class is a better source.