I am trying to get data from a REST api that uses a token for authentication in the URL. I can pass the relevant parameters in a web browser and get the data with no problem, however as soon as I try to do it in a PHP script with curl, I get a 401 error saying http authentication is required.
Have tried many different options but cannot get it to work, any help would be appreciated. My code is below, have removed site id and changed api_key
API documentation says:
The API can be accessed via HTTPS protocol only. SolarEdge monitoring server supports both HTTPS/1.0 and HTTPS/1.1 protocols.
- All APIs are secured via an access token:
Every access to the API requires a valid token as a URL parameter named api_key.
For example: api_key=L4QLVQ1LOKCQX2193VSEICXW61NP6B1O
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0, CURLINFO_HEADER_OUT => 1, CURLOPT_POST => 1, CURLOPT_HTTPAUTH, CURLAUTH_ANY, CURLOPT_URL => 'https://monitoringapi.solaredge.com/site/?????/energyDetails', CURLOPT_USERPWD, 'api_key:6X0kjehfdgksdljsgksdjhfksdhfglksd', CURLOPT_HTTPHEADER=>array( "timeUnit:DAY", "meters=PRODUCTION,CONSUMPTION", "startTime:2017-12-09 00:00:00", "endTime:2017-12-09 23:59:59"), )); $resp = curl_exec($curl); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Close request to clear up some resources curl_close($curl); echo $resp; echo "<br>$status_code"; //var_dump ($resp); //print_r ($resp); ?>
