3

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);


?>
1
  • Do you get results when you try your script locally? Commented Feb 16, 2018 at 4:32

1 Answer 1

3

Check the documentation again. Search for (Basic) Authentication.
You should look for something like this:
Authorization: <type> <credentials>
e.g. Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l

The HTTP 401 Unauthorized client error status response code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.

This status is sent with a WWW-Authenticate header that contains information on how to authorize correctly.

This status is similar to 403, but in this case, authentication is possible.

enter image description here

Try your code again like this (add your API key) and report back the results:

$curl = curl_init();
curl_setopt_array($curl, array(
    // CURLOPT_PORT=> 443,
    // 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($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo $resp;
echo "<br>$status_code";
Sign up to request clarification or add additional context in comments.

1 Comment

so using the above code it no longer throws the error, but it also doesn't produce any output whatsoever. Also if I set returntransfer=1 so i can get the data in a variable, it throws the error. The documentation makes no mention of basic or any other type of authentication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.