API Examples

PHP

Below is a simple example of php code that uses curl to retrieve data from the API in order to list the Nobel Laureates in Physiology or Medicine between 1990 and 1994. For simplicity, it’s kept short and has no error handling.

The php code:


<?php
# Base URL for the API
$url  = ‘http://api.nobelprize.org/v1/prize.json’;
# List Nobel Prizes in Medicine from 1990 to 1994
$url .= ‘?category=medicine&year=1990&yearto=1994’;

# Retreieve the result from the API using CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$string = curl_exec($ch);
curl_close($ch);

# Read the JSON output into an associative array
$result  = json_decode($string, true);

# Find out how many years that are listed
$numyears = count($result[‘prizes’]);

for ($i = 0; $i < $numyears; $i++) {

# Print out the category and year
$cat = $result[‘prizes’][$i][‘category’];
$year = $result[‘prizes’][$i][‘year’];
print “<b>$cat $year</b><br>n”;

# Find out how many laureates this category and year
$numlaur = count($result[‘prizes’][$i][‘laureates’]);

for ($j = 0; $j < $numlaur; $j++) {

#   Print out the names
$firstname = $result[‘prizes’][$i][‘laureates’][$j][‘firstname’];
$surname = $result[‘prizes’][$i][‘laureates’][$j][‘surname’];
print “$firstname $surname <br>n”;

}
print “<br>n”;
}

?>

 

The result:

medicine 1994
Alfred G. Gilman
Martin Rodbell

medicine 1993
Richard J. Roberts
Phillip A. Sharp

medicine 1992
Edmond H. Fischer
Edwin G. Krebs

medicine 1991
Erwin Neher
Bert Sakmann

medicine 1990
Joseph E. Murray
E. Donnall Thomas

 

To cite this section
MLA style: API Examples. NobelPrize.org. Nobel Prize Outreach AB 2024. Sat. 21 Dec 2024. <https://www.nobelprize.org/organization/api-examples/>