Department of Labor Logo United States Department of Labor
Dot gov

The .gov means it's official.
Federal government websites often end in .gov or .mil. Before sharing sensitive information, make sure you're on a federal government site.

Https

The site is secure.
The https:// ensures that you are connecting to the official website and that any information you provide is encrypted and transmitted securely.

Developer's Site
PRINT:Print

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 = 'https://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 = 'https://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