Degree Days

Degree Days

Weather Data for Energy Professionals

Weather Underground

Access the Degree Days.net API using PHP

This page has some sample code showing how to use PHP to specify an XML request, send it to our API servers (past the security system), and get an XML response back.

It's not a full client library (like we have for Java, .NET, and Python), but it should work fine so long as you can specify your request in XML, and write some code to parse the XML response.

See the XML API docs for more information about how to specify the data you want in the XML request. There are lots of options, and the XML included in the PHP code below is just a very simple example to get an API request working.

<?php

// The test API access keys are described at http://www.degreedays.net/api/test
// They'll let you access data from the Cape Cod area only.
// Enter your own API access keys to fetch data from locations worldwide.
$accountKey = 'test-test-test';
$securityKey = 'test-test-test-test-test-test-test-test-test-test-test-test-test';
$url = 'http://apiv1.degreedays.net/xml';
$timestamp = gmdate('c');
$random = uniqid();

// See http://www.degreedays.net/api/xml for more on the options you can specify
// in the XML request.  The example below is very basic; you can also:
//  - fetch data from a specific station ID, longitude/latitude location, or
//    postal code (for countries worldwide).
//  - fetch daily/weekly/monthly/yearly/average HDD and CDD covering a period
//    of your choice.
//  - fetch HDD and CDD, in multiple base temperatures, all in one request.
$requestXml = '
<RequestEnvelope>
    <SecurityInfo>
        <Endpoint>' . $url . '</Endpoint>
        <AccountKey>' . $accountKey . '</AccountKey>
        <Timestamp>' . $timestamp . '</Timestamp>
        <Random>' . $random . '</Random>
    </SecurityInfo>
    <LocationDataRequest>
        <PostalCodeLocation>
            <PostalCode>02532</PostalCode>
            <CountryCode>US</CountryCode>
        </PostalCodeLocation>
        <DataSpecs>
            <DatedDataSpec key="dailyHDDForLast7Days">
                <HeatingDegreeDaysCalculation>
                    <FahrenheitBaseTemperature>65</FahrenheitBaseTemperature>
                </HeatingDegreeDaysCalculation>
                <DailyBreakdown>
                    <LatestValuesPeriod>
                        <NumberOfValues>7</NumberOfValues>
                    </LatestValuesPeriod>
                </DailyBreakdown>
            </DatedDataSpec>
        </DataSpecs>
    </LocationDataRequest>
</RequestEnvelope>';

// To get hash_hmac (below) to work on older versions of PHP (pre-5.1.2) you may
// need to install the PECL HASH extension.  On Windows we found we needed to
// enable it by adding the following line to the appropriate section of php.ini:
// extension=php_hash.dll
$signatureBytes = hash_hmac('sha256', $requestXml, $securityKey, true);

function base64url_encode($unencoded) { 
    return rtrim(strtr(base64_encode($unencoded), '+/', '-_'), '='); 
}

$requestParameters = array(
    'request_encoding' => 'base64url',
    'signature_method' => 'HmacSHA256',
    'signature_encoding' => 'base64url',
    'encoded_request' => base64url_encode($requestXml),
    'encoded_signature' => base64url_encode($signatureBytes)
);

// If curl_init() fails, you may need to add or uncomment the following line in
// the appropriate section of php.ini:
// extension=php_curl.dll
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestParameters));
if (defined('CURLOPT_ENCODING')) {
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responseXml = curl_exec($ch);
curl_close($ch);

// You'd want to parse $responseXml, but for this example we'll just echo it
// out for display.  Setting the content type to XML makes it look neat in the
// browser.  (Make sure to remove this line if you're not echoing the XML.)
header('Content-type: application/xml');
echo $responseXml;

?>

Hopefully the example code above and the XML API docs will be enough to get you going, but please feel free to email us if you'd like more help.

It is also worth reading the higher-level integration guide for tips on the various approaches to integrating with the API. We have helped a lot of businesses integrate their software with our API so we are very familiar with the patterns that work well for common use cases.

Choose your Plan and Sign Up Today!

© 2018 BizEE Software Limited - About | Contact | Web Tool | API | Integration Guide | API FAQ | API Sign-Up