0

OK so im a noob when it comes to APi's. I have used wundergrounds api with out a problem. However im trying to use LogMeIn's REST API with php and i must be doing something wrong.

The instruction say to Check your Credentials

The first call should be GET /v1/authentication to check if your credentials are valid. So if your CompanyID is 1234567890 and your PSK is abcde12345ABCDE12345 (normally an actual PSK would be 128 characters long), the action can be called as:

$ curl https://secure.logmein.com/public-api/v1/authentication -i -H "Accept: application/JSON; charset=utf-8" -H "Authorization: {\"companyId\": 1234567890, \"psk\": \"abcde12345ABCDE12345\"}"

Where the -H argument sends a HTTP header and the -i argument shows the header of the response.

Response:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 16
Content-Type: application/JSON; charset=utf-8
Server: Microsoft-IIS/7.5
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"'
X-Powered-By: ASP.NET
Date: Mon, 11 Nov 2013 15:12:57 GMT

{"success":true}

so here is my PHP:

<?php
$json_string_LogMeIn = file_get_contents("https://secure.logmein.com/public-api/v1/authentication -i -H "Accept: application/JSON; charset=utf-8" -H "Authorization: {\"companyId\": 1234567890, \"psk\": \"abcde12345ABCDE12345\"}"");
$parsed_json_LogMeIn = json_decode($json_string_LogMeIn);
echo $parsed_json_LogMeIn;
?>

I added my company code in place of 1234567890 and my generated psk in place of abcde12345ABCDE12345

when i open the php page i get nothing

2 Answers2

0

file_get_contents() doesn't work like you're trying to, use cURL instead, i.e.:

$headers = array(
    "Accept: application/JSON; charset=utf-8",
    "Authorization: {\"companyId\": 1234567890, \"psk\": \"abcde12345ABCDE12345\"}",
);
$url = "https://secure.logmein.com/public-api/v1/authentication";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
# {"success":false} / which is correct since I'm using a fake Authorization

Update:

It seems that you need to enable curl on php.ini, please refer to this link

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • This is the error in getting: **11 Fatal error: Uncaught Error: Call to undefined function curl_init()** *in my php.ini:* **[ExtensionList] extension=php_mysql.dll extension=php_mysqli.dll extension=php_mbstring.dll extension=php_gd2.dll extension=php_gettext.dll extension=php_curl.dll extension=php_exif.dll extension=php_xmlrpc.dll extension=php_openssl.dll extension=php_soap.dll extension=php_pdo_mysql.dll extension=php_pdo_sqlite.dll extension=php_imap.dll extension=php_tidy.dll** – Nrodgers1987 May 03 '17 at 10:47
  • Hmm, There is no ";" in front of "extension=php_curl.dll" so it's enabled in my php.ini and showing enabled when i look at the PHP manager in IIS and the dll is where extension_dir="C:\Program Files\PHP\v7.0\ext\" I am running iis7 and php 7.0.0. When i "phpinfo()" i do not see curl listed. i have two web servers set up the same way and getting the same result on both. – Nrodgers1987 May 03 '17 at 12:31
  • You may have mulltiple php.ini. Check the location of the php.ini being used on php_info(). – Pedro Lobito May 03 '17 at 12:58
  • I checked and made sure i clicked on the php.ini inside the php manager, the php manager shows it is enabled. – Nrodgers1987 May 03 '17 at 14:26
0

So i did a few things to fix my issue:

1st i upgraded to a newer version of PHP 7.1.1 which fixed Curl, then i was getting a cert error. i found a fix for that as well.

Your code works though i switched it up a little.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://secure.logmein.com/public-api/v1/authentication");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array();
$headers[] = "Accept: application/JSON; charset=utf-8";
$headers[] = "Authorization: {\"companyId\": 1234567890, \"psk\": \"abcde12345ABCDE12345\"}";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;