back to phpList.org

Making Rest API call


#1

Ok. Excuse my ignorance.

Now that I’ve gotten the curl https://domain.com/lists/api/v2/sessions working, how exactly is it that I would make a call to it to add an email/name/data to a list/database for PHPList? Is there a specific curl command I can use to add a subscriber to a list? I ask because the link to examples in the manual doesn’t work.

“To demonstrate how to use the API some example clients are available in different programming languages. See the REST API Client Examples repository for details.”

The link in that line goes right back to the same page its on - no examples.

I did find something on GitHub, but it appears to reference it’s own code. I’m guessing that we should be able to call the API directly with some php or curl to do something like the above request?


#2

Here’s a simple example on GitHub:


When you consume the other calls in your implementation make sure to use the basic auth header.
Let me know if you have other questions.


#3

I tried that code. I got it to work once, then it didn’t work again. First question I have is: Shouldn’t it be referencing the API instead of installing a whole new framework, as this has a “vendor” folder that seems to be required for that example (and I put it on my server and got it to work once, but couldn’t get it to work again).

It seems like we should be referencing the API installed by PHPList, no?


#4

Probably the session token expired.
That’s simply an example of a client implementation that uses the phpList REST API, but developers can choose to implement it in whatever programming language or framework works best for them.
You can take a look at API Blueprint for the complete documentation of the supported requests.


#5

Ok, I’ve got your Rest API example working every time. I was making a call to an incorrect URI.

Here’s the next question: When I’m adding a subscriber, how to do I make sure they go onto a specific list - I can’t seem to find a way to do it with your subscriberRequest function.

2nd Question: is there a way to add a particular attribute as well, when adding a subscriber?


#6

Look at post above. Please.


#7

Great that it’s working!

They are not yet supported.


#8

That’s disappointing. Any time line on when that might happen?


#9

Hi Jody - it looks alike I am in the same situation as you. I would like to use phplists for a non profit association to send newsletter to our members. I would like to invite each member automatically during the registration process.
Right mow I’m completely lost - as I do not even get the example from xheni returning me anything.
What did you do to get it the Rest API example working every time?
Any hint would be appreciated.
My phpList is on revision 3.4.2 and when I try to create a session I get an error code 400 - “Empty JSON data”


#10

Make sure that the content-type of your request is set to Json.


#11

Thanks for your help - but for my very simple tests with Postman I made sure, that the content-type is set to Json but the return is always empty code 400 ‘Empty JSON data’
I am also playing around with the example from Xheni Myrtaj. The funny thing is, that it partially worked for phpList 3.4.1 - but after updating to 3.4.2 I cannot even start a session, getting error code 500 ‘Internal Server Error’


#12

Did you check that the rest api is still enabled after the update?


#13

Hi D,

My server is running on NGINX. In another thread I was having trouble connecting to the API. I’m not sure if you’re running NGINX, but it was all of a simple rewrite line that fixed my issue - to direct the API to know where to look for the page/file. That’s because NGINX doesn’t use .htaccess files. Code wise it was as I mentioned a single line compared to all the code the .htaccess file has in the example.


#14

@samtuke - YES, after the update I enabled the rest api again - but no success.

@jodywhitesides - Our association server is hosted from a German provider - so I do not really know what is behind the ‘wall’. mod_rewrite is enabled there and it supports .htaccess files as well.
However - I did something similar with the phpscheduleit/booked api and that is working like a charme.
I am really struggeling with phpList 3.4.2


#15

Make sure that your URL is correct.


#16

Hello xheni - how can I use wrong URLs by using your example?

I have taken your example code, changed the domain to the one of our server, like

//Please replace the following values with yours.
$loginname = ‘my admin login’;
$password = ‘my admin password’;
$base_uri = ‘https://makerspace-wi.de/lists/api/v2’;

and so on.

When I start the script

try {
$response = $client->request(‘POST’, $base_uri . ‘/sessions’, [
‘form_params’ => [
‘login_name’ => $loginname,
‘password’ => $password,
],
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
}

it already throws the exception:
{“code”:500,“message”:“Internal Server Error”}

Any help would be highly appreciated.


#17

I tried your example with incorrect credentials and I get
GuzzleHttp\Exception\ClientException: Client error:POST https://makerspace-wi.de/lists/api/v2/sessionsresulted in a401 Unauthorizedresponse: {"code":401,"message":"Unauthorized"}
which means the url is correct.
Try adding echo $e; in catch (\GuzzleHttp\Exception\GuzzleException $e) {
}


#18

Hi xheni,

I tried this already with correct credentials and get:
Server error: POST https://makerspace-wi.de/lists/api/v2/sessions resulted in a 500 Internal Server Error


#19

Seems like you must be using a different request to Xheni.