YogiBear
YogiBear

Reputation: 31

PHP Curl Authentication Issue using API

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.

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


?>

Upvotes: 3

Views: 1144

Answers (1)

Panos Kalatzantonakis
Panos Kalatzantonakis

Reputation: 12673

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

Upvotes: 3

Related Questions