laren0815 laren0815 - 4 months ago 18
PHP Question

openfire rest api get request is empty

I have the XMPP-server openfire.
Also i have added the "rest api plugin" and tested it with the Chrome Tool "Postman", and it works fine.

Request:

http://myhost.net:9090/plugins/restapi/v1/users


Header:

Authorization:myPasswort


Result:

{
"user": [
{
"username": "admin",
"name": "Administrator",
"email": "info@admin.de",
"properties": null
},
...


Now i want to use the result in a my composer app from appgyver.com. Same input, but always the server result:

Response code: 500. {"error":"Internal server error."}

Then i try a get request with a php script:

$url = "myhost.net/plugins/restapi/v1/users";

$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'Authorization' => 'myPasswort'
),
);
$context = stream_context_create($options);
var_dump($http_response_header);
echo "\r\n";
$result = file_get_contents($url, false, $context);
var_dump($result);


with the result: NULL bool(false)

What i doing wrong?
Thanks

Answer

Try it with follow php snippet:

    <?php 
      $url = "http://localhost:9090/plugins/restapi/v1/users";
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_HEADER, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array("Accept application/json", "Authorization: PehtV8mbAm5M5MM0"));
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
      $json_response = curl_exec($curl);
      $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      echo $json_response;
      curl_close($curl);
   ?>

Just change to your URL and the authorization key.

Comments