Developer's Site

Accessing the Public Data API with PHP Code

On This Page:

API Version 2.0 PHP Sample Code

Multiple Series and Multiple Years

Use this code to retrieve data for more than one timeseries and more than one year.


        $url = 'http://api.bls.gov/publicAPI/v2/timeseries/data/';
        $method = 'POST';
        $query = array(
                'seriesid'  => array('LEU0254555900', 'APU0000701111'),
                'startyear' => '2002',
                'endyear'   => '2012'
        );
        $pd = json_encode($query);
        $contentType = 'Content-Type: application/json';
        $contentLength = 'Content-Length: ' . strlen($pd);

        $result = file_get_contents(
                $url, null, stream_context_create(
                        array(
                                'http' => array(
                                        'method' => $method,
                                        'header' => $contentType . "\r\n" . $contentLength . "\r\n",
                                        'content' => $pd
                                ),
                        )
                )
        );

        var_dump($http_response_header);
        var_dump($result);

    

API Version 1.0 PHP Sample Code

Multiple Series and Multiple Years

Use this code to retrieve data for more than one timeseries and more than one year.


        $url = 'http://api.bls.gov/publicAPI/v1/timeseries/data/';
        $method = 'POST';
        $query = array(
                'seriesid'  => array('LEU0254555900', 'APU0000701111'),
                'startyear' => '2002',
                'endyear'   => '2012'
        );
        $pd = json_encode($query);
        $contentType = 'Content-Type: application/json';
        $contentLength = 'Content-Length: ' . strlen($pd);

        $result = file_get_contents(
                $url, null, stream_context_create(
                        array(
                                'http' => array(
                                        'method' => $method,
                                        'header' => $contentType . "\r\n" . $contentLength . "\r\n",
                                        'content' => $pd
                                ),
                        )
                )
        );

        var_dump($http_response_header);
        var_dump($result);

    

Last Modified Date: October 16, 2014

Recommend this page using: