alextoft / sureflap

Basic PHP Examples for SureFlap API (IoT cat flap)
73 stars 17 forks source link

No Connect to https://app.api.surehub.io #10

Closed staebchen0 closed 5 years ago

staebchen0 commented 5 years ago

Hi everyone, i am new and i do not know php. When trying to establish the connection, I get the following message:

wamp_Meldung

where is the mistake? Is the call in code line 7 wrong? If so, how should I change this?

System: Windows 8, PHP7, wamp 3.1.3

Best regards Anja

Mattheo1985 commented 5 years ago

Hi Anja,

try to create an empty file named "token" in the folder C:\wamp\www\Home\

Any difference?

staebchen0 commented 5 years ago

Hi Matthias,

i create an empty file token.txt but there ist no difference. It was a long time before the same message came again.

Gruß Anja

Mattheo1985 commented 5 years ago

It must be named just "token" without file extension.

staebchen0 commented 5 years ago

okay, now comes only the message: Curl Failed

hdurdle commented 5 years ago

If you have a default install of the WAMP stack you may not have CURL enabled. Try:

Close WAMP Navigate to WAMP\bin\php(your version of php)\ Edit php.ini Search for curl, uncomment extension=php_curl.dll Navigate to WAMP\bin\Apache(your version of apache)\bin\ Edit php.ini Search for curl, uncomment extension=php_curl.dll Save both Restart WAMP

staebchen0 commented 5 years ago

239/5000 in both ini files I have extension = php_curl.dll entered. In the php.ini of Apache this entry already existed. not in the php.ini. After the restart of wamp, however, I get the same message as above ....

hdurdle commented 5 years ago

A hunch... try adding these lines:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

after this line:

$ch = curl_init($endpoint."/api/auth/login");

If that works, it means the WAMP stack isn't configured to properly deal with SSL secured sites.

staebchen0 commented 5 years ago

Now this error comes: Notice: Undefined index: data in C:\wamp\www\Home\login.php on line 23

here the line 23 bold: if($result['data']['token']) { print "New Login Successful for ".$result['data']['user']['first_name']." ".$result['data']['user']['last_name']."!\nToken: ".$result['data']['token']; $token = $result['data']['token']; file_put_contents("token",$token); } else { die("Login Failed!\n"); }

print "\n";

hdurdle commented 5 years ago

Looks like you're not receiving a successful login response from the API. Have you put the correct email_address and password for your SurePet login in a file called config.php ? (That should be a copy of the config.php.dist that you edit with your details.)

hdurdle commented 5 years ago

Also, you'll either need to fix your WAMP setup, or put that curl change I showed you in every page so it ignores SSL. (The latter being the wrong way to deal with it!)

staebchen0 commented 5 years ago

yes, I have created a copy of the file "config.php.dist", entered my e-mail address and PW and saved the file under the name "config.php".

When I enter the credentials on the page: https://www.surepetcare.io/ enter, then the access works.

I'll try to reinstall wamp, maybe I made a mistake here.

hdurdle commented 5 years ago

One last thing - try deleting that token file and trying again - when the code is working, it will create that file when it is needed.

staebchen0 commented 5 years ago

If I delete the file, comes again the first error message:

Warning: file_get_contents(token): failed to open stream: No such file or directory in C:\wamp\www\Home\login.php on line 7

I think I have to deal with the PHP installation and Wamp installation again Is there perhaps a guide for the windows installation?

hdurdle commented 5 years ago

That warning is fine though - it is just a warning- it doesn’t impact the running of the code. What happens after that warning?

I just tried the raw code on PHP in Ubuntu under Windows and it behaves as expected.

staebchen0 commented 5 years ago

So, Wamp is reinstalled. PHP. ini file Curl is active. I get another message now when I run login.php: Parse error: syntax error, unexpected 'logout' (T_STRING) in C:\wamp64\www\home\login.php on line 9

the token file was not created again. I have created these again, but get the same message as without a file

Line 9: include 'logout.php'; The File is in the home folder!

One more note: I did not install Ubuntu. I try the whole thing with a Windows operating system.

hdurdle commented 5 years ago

What version of Windows and what version of WAMP did you install? Where did you download WAMP from?

staebchen0 commented 5 years ago

Windows 8.1 - 64 bit WampServer Version 3.1.7 64bit Download WAMP: http://www.wampserver.com/en/download-wampserver-64bits/ Apache 2.4.37 Port 80- PHP 7.2.14 MySQL 5.7.24 Port 3306 MariaDB 10.3.12 Port 3307 PHP 5.6.40 for CLI (Command-Line Interface)

staebchen0 commented 5 years ago

Info: I have only installed the included PHP etc. components of WAMP. No PHP, CURL additionally installed

hdurdle commented 5 years ago

Cool. Busy today, but I’m genuinely curious as to what is happening here so will spin it up and take a look. (This isn’t even my repo/code!)

staebchen0 commented 5 years ago

Great, thank you for your help :-)

staebchen0 commented 5 years ago

Hi,

I tested the following: Value set to 2 curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 2);

Then a token was created (in the file "Token")

If I then set the parameter back to 'false': curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, False)

Report: Found Old Session Token. Attempting Logout ... Token Invalid / Expired! New Login Successful for Test Python! Token: eyJ0eXAiOiJKV1QiLCJhbGciZGV2aWNlX2lkIjoiMjU1Nzc3NzIwNCJ etc ....

staebchen0 commented 5 years ago

Value set to 1: curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 1);

Report: ( ! ) Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in C:\wamp64\www\home\login.php on line 10

1 | 0.0012 | 401176 | {main}( ) | ...\login.php:0 2 | 0.5749 | 402992 | curl_setopt ( ) | ...\login.php:10

New Login Successful for Test Python ! Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.....etc.

staebchen0 commented 5 years ago

I tested the following: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_CAINFO, "C:/wamp64/apps/phpmyadmin4.8.4/libraries/certs/cacert.pem");

Report: Found Old Session Token. Attempting Logout... Token Invalid/Expired! Curl Failed

staebchen0 commented 5 years ago

which I do not understand: the login has supposedly worked and the token is displayed.

The same token is also in the file - and yet I can not run another script successfully.

Example: getCurfewStatus.php

Found Old Session Token. Attempting Logout ... Token Invalid / Expired! New Login Successful for Test Python! Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 ...... etc.

hdurdle commented 5 years ago

Okay, here's what you need to do: with a clean install of WAMP Server, and with the code from the sureflap repository with NO changes:

Following these steps, the code in the repo works without any changes.

Basically we just configured PHP on Windows to behave as it would out of the box on a Linux host!

Hope that helps.

staebchen0 commented 5 years ago

Aufruf: http://localhost/sureflap-master/login.php Antwort: ! ) Warning: file_get_contents(token): failed to open stream: No such file or directory in C:\wamp64\www\sureflap-master\login.php on line 7

1 | 0.0004 | 400088 | {main}( ) | ...\login.php:0 2 | 0.0007 | 400184 | file_get_contents ( ) | ...\login.php:7

I dont understand ;-(

staebchen0 commented 5 years ago

here the php.ini with changes:

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo = "C:\wamp64\bin\php\php7.3.1\cacert.pem"

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile= "C:\wamp64\bin\php\php7.3.1\cacert.pem"
staebchen0 commented 5 years ago

I think creating the token file does not work. for whatever reason...

when I create the file and login.php calls comes again Curl Failed

hdurdle commented 5 years ago

Two things-

One: you didn’t update the two php.ini files I mentioned. You updated one in an Apache directory instead. Look for the path to the php version you’re using and update both php.ini and phpForApache.ini.

Two: are you trying to open these scripts in a web browser? I suspect they were written with the command line in mind, and that’s how I’ve been testing them.

From a command line try running: <path to php.exe> login.php

staebchen0 commented 5 years ago

okay, I understand, but apart from the included php installation in WAMP I have no further PHP installation on the machine. That's why I only adapted the php.ini in the WAMP directory. This phh.ini File in: C:\wamp64\bin\php\php7.3.1 and C:\wamp64\bin\apache\apache2.4.37\bin

I thought the php installation that comes with WAMP is enough. Then I will install PHP And yes, I execute the commands in the browser. I'll test that first now

Thank you for your patience :-)

hdurdle commented 5 years ago

If you did a standard install of the WAMP Server you already have everything you need. You can open a command prompt, navigate to where you put the repo files and run:

C:\wamp64\bin\php\php7.3.1\php.exe login.php

You can make this easier by adding the php path to your path environment variables. Then you can just run:

php <anyfile>

from any location.

staebchen0 commented 5 years ago

well, I have yet discovered a mistake. I have made the changes to the php.ini version 7.3.1. In the WAMP server, however, version 7.2.14 was active.

If I now execute the command in the DOS window, the following message appears:

C:\wamp64\bin\php\php7.2.14>php.exe C:\wamp64\www\sureflap-master\login.php

PHP Warning:  file_get_contents(token): failed to open stream: No such file or directory in C:\wamp64\www\sureflap-master\login.php on line 7

Warning: file_get_contents(token): failed to open stream: No such file or directory in C:\wamp64\www\sureflap-master\login.php on line 7
PHP Fatal error:  Uncaught Error: Call to undefined function curl_init() in C:\wamp64\www\sureflap-master\login.php:14
Stack trace:
#0 {main}
  thrown in C:\wamp64\www\sureflap-master\login.php on line 14

Fatal error: Uncaught Error: Call to undefined function curl_init() in C:\wamp64\www\sureflap-master\login.php:14
Stack trace:
#0 {main}
  thrown in C:\wamp64\www\sureflap-master\login.php on line 14
hdurdle commented 5 years ago

Put the source files (this repo) somewhere under your Documents directory and try again.

hdurdle commented 5 years ago

It doesn't matter which version of PHP you use, you just need to be sure that:

If you don't care about using PHP and just want to see data from the cat flap on Windows, try these Powershell scripts: https://github.com/hdurdle/sureflap

staebchen0 commented 5 years ago

so, now i have wamp reinstalled and the change php. ini and phpForApache.ini made.

now it seems to work if I run the command at DOS level :-)

I would like to thank you very much for your help !!!!

hdurdle commented 5 years ago

Great - @alextoft I think you can close this now. Not an issue with your code! :)

staebchen0 commented 5 years ago

Thanks to Howard