Accessing the NPR API using PHP

From Knowledge Kitchen
Jump to navigation Jump to search


National Public Radio (NPR) makes a available an API for accessing NPR's audio shows. Given that NPR is primarily a radio station, those stories come in the form of audio files. Like many older APIs built by non-commercial entities, the NPR API packages this data as XML.

Using cURL to make HTTP requests

These examples rely on the usage of PHP's built-in cURL library, which allows PHP programs to send HTTP requests to web servers.

Determining the URL to request from the API

NPR provides a convenient tool to use to generate the URLs to use when making requests to their API.

Authentication

NPR's API requires application developers to register online in order to receive an API key used to authenticate your application's requests to the API. This API key is then placed in the query string of the URL that cURL uses to make requests to the API.

Retrieving raw XML data

This example pulls the raw XML data from the NPR API. It is not formatted or presented in a very usable way.

 1 <?php
 2 /*
 3  *
 4  * This example exhibits pulling data from the NPR API
 5  * The data comes packaged as XML.  In a real-world application, you would parse that XML and display some of the data as regular HTML in a regular webpage.
 6  *
 7  * See official NPR API documentation here: http://www.npr.org/api
 8  * Test out the API calls here: http://www.npr.org/api/queryGenerator.php
 9  *
10  */
11 
12 //api url we got by testing from their console available at: http://www.npr.org/api/queryGenerator.php
13 //notice that our unique api key that we get when registering is part of the query string
14 $apiURL = "http://api.npr.org/query?id=1056&apiKey=your_api_key";
15 
16 //use the PHP curl library to request data from the API
17 $ch = curl_init();
18 
19 //set URL to request
20 curl_setopt($ch, CURLOPT_URL, $apiURL);
21 
22 //tell curl not to output the response automatically
23 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24 
25 //set the user agent string that is sent with the request
26 //our program is pretending to be Google Chrome
27 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
28 
29 //execute the http request and store the response
30 $response = curl_exec($ch);
31 
32 //send to the browser the data we get back from the request
33 echo $response; //this is for debugging only
34 
35 //in a real application, you would now extract the parts of this data that you were interested in, and display it nicely using HTML and CSS to make it look nice.
36 //to do this would require you to parse the XML code
37 //there are ready-to-use XML parser libraries available in all major programming languages
38 //see the other file in this folder for an example
39 
40 ?>


Presenting the data as well-formed HTML

This example pulls the raw XML data from the NPR API. That data is then used to create a well-formatted HTML document.

 1 <?php
 2 /*
 3  * This example exhibits pulling data from the NPR API
 4  * The data comes packaged as XML.  We then parse that XML and display some of the data as regular HTML in a regular webpage.
 5  *
 6  * See official NPR API documentation here: http://www.npr.org/api
 7  * Test out the API calls here: http://www.npr.org/api/queryGenerator.php
 8  *
 9  */
10 
11 //api url we got by testing from their console
12 //notice that our unique api key that we get when registering at NPR.org is part of the query string
13 $apiURL = "http://api.npr.org/query?id=1056&apiKey=your_api_key";
14 
15 //use the PHP curl library to request data from the API
16 $ch = curl_init();
17 
18 //set URL to request
19 curl_setopt($ch, CURLOPT_URL, $apiURL);
20 
21 //tell curl not to output the response automatically
22 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23 
24 //set the user agent string that is sent with the request
25 //our program is pretending to be Google Chrome
26 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
27 
28 //execute the http request and store the response
29 $response = curl_exec($ch);
30 
31 //convert the XML into a usable PHP object using PHP's built-in SimpleXML Parser
32 $data = simplexml_load_string($response); 
33 
34 //see usage of this data embedded in the HTML below:
35 
36 ?>
37 <!DOCTYPE html>
38 <html>
39 	<head>
40 		<title>Example Usage of NPR API</title>
41 		<style>
42  div#wrapper {
43  	width: 940px; /* fix width of page */
44  	margin: 0 auto; /* center page */
45  	border: 1px solid #c0c0c0; /*light gray border around page */
46  	padding: 20px;
47  }
48  section#data article {
49  	padding: 10px; /* space out articles */
50  	border-bottom: 1px solid #c0c0c0; /* divider between articles */
51  } 
52 		</style>
53 	</head>
54 	<body>
55 		<div id="wrapper">
56 			<header>
57 				<h1>Example usage of the NPR API</h1>
58 				<p>
59 					This is a nicer version of <a href="index.php">the raw XML scraper version</a>.
60 				</p>.
61 			</header>
62 			<section id="data">
63 				<h1><?php echo $data->list->title; ?></h1>
64 				<p>Why do they give this data away for free?</p>
65 
66 			<!-- begin loop through all the story tags in the XML object -->
67 
68 <?php foreach ($data->list->story as $story) : ?>
69 
70 				<article>
71 					<!-- output the heading for this story -->
72 					<h1><?php echo $story->title; ?></h1>
73 
74 					<!-- output the teaser for this story -->	
75 					<p>
76 						<?php echo $story->teaser; ?>
77 						...<a href="<?php echo $story->link; ?>">more at NPR.org</a>...
78 					</p>
79 
80 					<!-- output the audio tag if there is audio for this story -->
81 					
82 	<?php if ($story->audio->format->mp4) : ?>
83 
84 					<audio src="<?php echo $story->audio->format->mp4; ?>" controls />
85 
86 	<?php endif; ?>
87 
88 				</article>
89 
90 <?php endforeach; ?>
91 
92 			<!-- end loop through stories -->
93 
94 			</section>
95 
96 		</div>
97 	</body>
98 </html>


What links here