This repository was archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathNPRAPI.php
More file actions
299 lines (260 loc) · 8.47 KB
/
Copy pathNPRAPI.php
File metadata and controls
299 lines (260 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* @file
* Defines basic OOP containers for NPRML.
*/
/**
* Defines a class for NPRML creation/transmission and retreival/parsing, for any PHP-based system.
*/
class NPRAPI {
// HTTP status code = OK
const NPRAPI_STATUS_OK = 200;
// Default URL for pulling stories
const NPRAPI_PULL_URL = 'http://api.npr.org';
// NPRML CONSTANTS
const NPRML_DATA = '<?xml version="1.0" encoding="UTF-8"?><nprml></nprml>';
const NPRML_NAMESPACE = 'xmlns:nprml=http://api.npr.org/nprml';
const NPRML_VERSION = '0.92.2';
/**
* Initializes an NPRML object.
*/
function __construct() {
$this->request = new stdClass;
$this->request->method = NULL;
$this->request->params = NULL;
$this->request->data = NULL;
$this->request->path = NULL;
$this->request->base = NULL;
$this->response = new stdClass;
$this->response->code = NULL;
}
function request() {
}
function prepare_request() {
}
// Edit - Function has no parameters. Without parameters, throws error
// Strict standards: Declaration of NPRAPIWordpress::send_request() should be compatible with NPRAPI::send_request()
function send_request($nprml, $post_ID ) {
}
function parse_response() {
$xml = simplexml_load_string($this->response->data);
if (!empty($xml->list->story)) {
$id = $this->get_attribute($xml->list->story, 'id');
}
$this->response->id = $id ? $id : NULL;
}
function flatten() {
}
// Edit - Function has no parameters. Without parameters, throws error
// Strict standards: Declaration of NPRAPIWordpress::create_NPRML() should be compatible with NPRAPI::create_NPRML()
function create_NPRML( $post ) {
}
/**
* Parses object. Turns raw XML(NPRML) into various object properties.
*/
function parse() {
if (!empty($this->xml)) {
$xml = $this->xml;
}
else {
$this->notices[] = 'No XML to parse.';
return;
}
$object = simplexml_load_string($xml);
$this->add_simplexml_attributes($object, $this);
if ( isset( $object->message ) && !empty($object->message)) {
$this->message = new StdClass;
$this->message->id = $this->get_attribute($object->message, 'id');
$this->message->level = $this->get_attribute($object->message, 'level');
}
if ( isset($object->list->story ) && !empty($object->list->story)) {
foreach ($object->list->story as $story) {
$parsed = new NPRMLEntity();
$this->add_simplexml_attributes($story, $parsed);
//Iterate trough the XML document and list all the children
$xml_iterator = new SimpleXMLIterator($story->asXML());
$key = NULL;
$current = NULL;
for($xml_iterator->rewind(); $xml_iterator->valid(); $xml_iterator->next()) {
$current = $xml_iterator->current();
$key = $xml_iterator->key();
if ($key == 'image' || $key == 'audio' || $key == 'link') {
// images
if ($key == 'image') {
$parsed->{$key}[] = $this->parse_simplexml_element($current);
}
// audio
if ($key == 'audio') {
$parsed->{$key}[] = $this->parse_simplexml_element($current);
}
// links
if ($key == 'link') {
$type = $this->get_attribute($current, 'type');
$parsed->{$key}[$type] = $this->parse_simplexml_element($current);
}
}
else {
//if ->$key exist, see if it's an array. if it is, add the next child.
if (!empty($parsed->{$key})){
//if it's not an array, make an array, add the existing element to it
if (!is_array($parsed->{$key})){
$my_array[] = $parsed->{$key};
$parsed->{$key} = $my_array;
}
// then add the new child.
$parsed->{$key}[] = $this->parse_simplexml_element($current);
}
else {
//The key wasn't parsed already, so just add the current element.
$parsed->{$key} = $this->parse_simplexml_element($current);
}
}
}
$body ='';
if (!empty($parsed->textWithHtml->paragraphs)) {
foreach ($parsed->textWithHtml->paragraphs as $paragraph) {
$body = $body . $paragraph->value . "\n\n";
}
}
$parsed->body = $body;
$this->stories[] = $parsed;
}
//if the query didn't have a sort parameter, reverse the order so that we end up with
//stories in reverse-chron order.
//there are no params and 'sort=' is not in the URL
if (empty($this->request->params) && isset($this->request->url) && !stristr($this->request->url, 'sort=')){
$this->stories = array_reverse($this->stories);
}
//there are params, and sort is not one of them
if (!empty($this->request->params) && !array_key_exists('sort', $this->request->params)){
$this->stories = array_reverse($this->stories);
}
}
}
/**
* Converts SimpleXML element into NPRMLElement.
*
* @param object $element
* A SimpleXML element.
*
* @return object
* An NPRML element.
*/
function parse_simplexml_element($element) {
$NPRMLElement = new NPRMLElement();
$this->add_simplexml_attributes($element, $NPRMLElement);
if (count($element->children())) { // works for PHP5.2
foreach ($element->children() as $i => $child) {
if ($i == 'paragraph' || $i == 'mp3') {
if ($i == 'paragraph') {
$paragraph = $this->parse_simplexml_element($child);
$NPRMLElement->paragraphs[$paragraph->num] = $paragraph;
}
if ($i == 'mp3') {
$mp3 = $this->parse_simplexml_element($child);
$NPRMLElement->mp3[$mp3->type] = $mp3;
}
}
else {
//if ->$i exist, see if it's an array. if it is, add the next child.
if (!empty($NPRMLElement->$i)){
//if it's not an array, make an array, add the existing element to it
if (!is_array($NPRMLElement->$i)) {
$my_array[] = $NPRMLElement->$i;
$NPRMLElement->$i = $my_array;
}
// then add the new child.
$NPRMLElement->{$i}[] = $this->parse_simplexml_element($child);
}
else {
$NPRMLElement->$i = $this->parse_simplexml_element($child);
}
}
}
}
else {
$NPRMLElement->value = (string)$element;
}
return $NPRMLElement;
}
/**
* Extracts value of a given attribute from a SimpleXML element.
*
* @param object $element
* A SimpleXML element.
*
* @param string $attribute
* The name of an attribute of the element.
*
* @return string
* The value of the attribute (if it exists in element).
*/
function get_attribute($element, $attribute) {
foreach ($element->attributes() as $k => $v) {
if ($k == $attribute) {
return (string)$v;
}
}
}
/**
* Generates basic report of NPRML object.
*
* @return array
* Various messages (strings) .
*/
function report() {
$msg = array();
$params = '';
if (isset($this->request->params)) {
foreach ($this->request->params as $k => $v) {
$params .= " [$k => $v]";
}
$msg[] = 'Request params were: ' . $params;
}
else {
$msg[] = 'Request had no parameters.';
}
if ($this->response->code == self::NPRAPI_STATUS_OK) {
$msg[] = 'Response code was ' . $this->response->code . '.';
if (isset($this->stories)) {
$msg[] = ' Request returned ' . count($this->stories) . ' stories.';
}
}
elseif ($this->response->code != self::NPRAPI_STATUS_OK) {
$msg[] = 'Return code was ' . $this->response->code . '.';
}
else {
$msg[] = 'No info available.';
}
return $msg;
}
/**
* Takes attributes of a SimpleXML element and adds them to an object (as properties).
*
* @param object $element
* A SimpleXML element.
*
* @param object $object
* Any PHP object.
*/
function add_simplexml_attributes($element, $object) {
if (count($element->attributes())) {
foreach ($element->attributes() as $attr => $value) {
$object->$attr = (string)$value;
}
}
}
}
/**
* Basic OOP container for NPR entity (story, author, etc.).
*/
class NPRMLEntity {
}
/**
* Basic OOP container for NPRML element.
*/
class NPRMLElement {
function __toString() {
return $this->value;
}
}