Degree Days

Degree Days

Weather Data for Energy Professionals

Weather Underground

Access the Degree 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.


// The test API access keys are described at
// 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 = '';
$timestamp = gmdate('c');
$random = uniqid();

// See 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 = '
        <Endpoint>' . $url . '</Endpoint>
        <AccountKey>' . $accountKey . '</AccountKey>
        <Timestamp>' . $timestamp . '</Timestamp>
        <Random>' . $random . '</Random>
            <DatedDataSpec key="dailyHDDForLast7Days">

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

// 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 | Privacy | Web Tool | API | Integration Guide | API FAQ | API Sign-Up