What am I doing wrong? I get no response from the API.
<?php
$query = array(' [
"email" => "test@gmail.com",
"group_names" => "",
"custom_message" => "" ]');
$paramArray['api_key'] = '....................';
$paramArray['api_username'] = "admin";
$url = sprintf
(
'%s://%s%s?%s',
'https',
'forum.exemple.com/',
'invites',
http_build_query($paramArray)
);
$invite = curl_init();
curl_setopt($invite, CURLOPT_URL, $url);
curl_setopt($invite, CURLOPT_POSTFIELDS, http_build_query($query));
curl_setopt($invite, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($invite);
curl_close($invite);
print_r($resp);
?>
blake
(Blake Erickson)
Mai 10, 2018, 9:13
3
This worked for me:
<?php
$fields = array(
'api_key' => 'a71cb5058c6be27e42806ad788bc7b0008af9c15170d1be1827a24c8e8334107',
'api_username' => 'blake.erickson',
'email' => 'test' . rand() . '@example.com',
'custom_message' => 'Please join my awesome forum'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:3000/invites");
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
print_r($server_output);
?>
4 « J'aime »
pfaffman
(Jay Pfaffman)
Mai 11, 2018, 12:59
4
Maybe use system user instead of admin? Did you set up a user called admin?
4 « J'aime »
Yes, I am using the appropriate userid.
Is port 3000 fixed or needs to be configured?
blake
(Blake Erickson)
Mai 14, 2018, 9:15
7
I’m only using port 3000 because I’m using my development environment. If you are in production it is on port 80 and you won’t need to specify that.
3 « J'aime »
I definitely do not know what I’m doing wrong. I followed his tips but it did not work. It does not answer anything.
blake
(Blake Erickson)
Mai 14, 2018, 10:04
9
We are happy to help but there could be an infinite list of things that is causing your issue and we need a little bit more info in order to help out in any way.
As an example of one of the things that could go wrong is that you might not have the curl php module installed or enabled on your computer.
In order to rule out any API/internet issues I would start with just using curl directly from the command line and not use php and see if you can get it to work. There are two example api calls in the docs that require authentication that you should start out with:
curl -X GET "http://127.0.0.1:3000/admin/users/list/active.json?api_key=714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19&api_username=discourse1"
and
curl -X POST "http://127.0.0.1:3000/categories" \
-H "Content-Type: multipart/form-data;" \
-F "api_key=714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19" \
-F "api_username=discourse1" \
-F "name=89853c20-4409-e91a-a8ea-f6cdff96aaaa" \
-F "color=49d9e9" \
-F "text_color=f0fcfd"
See if you are able to run those successfully against your discourse site and we can go from there.
2 « J'aime »
“/admin/users/list/active.json” and “/categories” work properly.
But “/invites” does not work.
blake
(Blake Erickson)
Mai 14, 2018, 11:33
11
Can you show me your curl command to the /invites
endpoint(feel free to edit out your api key).
Here is the command I just used and you can see the successful json response:
blake@w:~$ curl -X POST "http://192.168.56.2:3000/invites" \
> -H "Content-Type: multipart/form-data;" \
> -F "api_key=b5d6568b19aee95ee5768e1f3929cbaa076545cba0427ecbee822a8114efde7c" \
> -F "api_username=system" \
> -F "email=asdfasdf@example.com" \
> -F "custom_message=asdfasdfasdf"
{"success":"OK"}blake@w:~$
2 « J'aime »
The response:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
blake
(Blake Erickson)
Mai 15, 2018, 12:16
13
Thank you for showing me the response. Can I also see the curl command you used? I’m not sure yet why nginx would return a 301.
Excuse me. I made a small mistake.
Calling CURL directly is working.
It is not working in PHP.
Falco
(Falco)
Mai 15, 2018, 1:36
15
Sounds like your PHP code isn’t following redirects, isn’t a matter of using HTTPS directly instead of HTTP?
3 « J'aime »
riking
(Kane York)
Mai 15, 2018, 5:11
16
Enable redirects using easy_setopt. CURLOPT_FOLLOWLOCATION
3 « J'aime »