| | 1 | = REST Interface == |
| | 2 | Resources are accessible via REST URIs to allow the creation of Mash-ups. |
| | 3 | |
| | 4 | == WordPress integration == |
| | 5 | Courtesy of timClicks. |
| | 6 | This example is for !HumanityRoad's !WordPress to pull in Hospitals data from Sahana Eden: |
| | 7 | {{{ |
| | 8 | <?php |
| | 9 | $download_error = False; |
| | 10 | $hospital_source_file = 'http://humanityroad.sahanafoundation.org/eden/hms/hospital.json'; |
| | 11 | |
| | 12 | $hospital_table_format = <<<HOSPITAL_TABLE_ROWS |
| | 13 | <tr class="hospital-info"> |
| | 14 | <td> |
| | 15 | <h4><a href="%s">%s</a></h4> |
| | 16 | <p>%s<br />%s</p> |
| | 17 | </td> |
| | 18 | <td> |
| | 19 | <h4> </h4> |
| | 20 | <dl class="hospital-d"> |
| | 21 | <dt>Phone</dt> |
| | 22 | <dd>%s</dd> |
| | 23 | <dt>Website:</dt> |
| | 24 | <dd>%s</dd> |
| | 25 | <dt>Email:</dt> |
| | 26 | <dd>%s</dd> |
| | 27 | </dl> |
| | 28 | </td> |
| | 29 | <td> |
| | 30 | <h4>Location</h4> |
| | 31 | <dl class="hospital-d"> |
| | 32 | <dt>Latitude</dt> |
| | 33 | <dd>%s</dd> |
| | 34 | </dt>Longitude</dt> |
| | 35 | <dd>%s</dd> |
| | 36 | </dl> |
| | 37 | </td> |
| | 38 | </tr> |
| | 39 | <tr class="hospital-comments"> |
| | 40 | <td colspan="3"> |
| | 41 | <dl class="hospital-d"> |
| | 42 | <dt>Comments:</dt> |
| | 43 | <dd>%s</dd> |
| | 44 | </dl> |
| | 45 | </td> |
| | 46 | </tr> |
| | 47 | <tr class="hospital-tech-details"> |
| | 48 | <td colspan="3"> |
| | 49 | <span>Modified by:</span> |
| | 50 | <span>%s</span> | |
| | 51 | <span>Last Modified</span> |
| | 52 | <span>%s</span> | |
| | 53 | <span>Global Identifier:</span> |
| | 54 | <span>%s</span> |
| | 55 | </td> |
| | 56 | </tr> |
| | 57 | <tr> |
| | 58 | <td colspan="3"><hr /></td> |
| | 59 | </tr> |
| | 60 | HOSPITAL_TABLE_ROWS; |
| | 61 | |
| | 62 | |
| | 63 | $head = <<<HEAD |
| | 64 | <!DOCTYPE html><html><head> |
| | 65 | |
| | 66 | <style> |
| | 67 | td { |
| | 68 | margin: 1em 0; |
| | 69 | padding: 0; |
| | 70 | } |
| | 71 | |
| | 72 | dt { |
| | 73 | position: relative; |
| | 74 | left: 0; |
| | 75 | top: 1.1em; |
| | 76 | width: 5em; |
| | 77 | font-weight: bold; |
| | 78 | font-family:monospace; |
| | 79 | } |
| | 80 | |
| | 81 | dl.hospital-d dd { |
| | 82 | border-left: 1px solid #000; |
| | 83 | margin: 0 0 0 6em; |
| | 84 | padding: 0 0 .5em .5em; |
| | 85 | } |
| | 86 | |
| | 87 | .hospital-tech-details { |
| | 88 | color:#aaa; |
| | 89 | font-size:0.8em; |
| | 90 | } |
| | 91 | </style> |
| | 92 | |
| | 93 | </head><body> |
| | 94 | HEAD; |
| | 95 | |
| | 96 | function process_comments($comment_string){ |
| | 97 | $a = explode("\r\n", $comment_string); |
| | 98 | foreach($a as &$para){ |
| | 99 | $para = $para.'<br />'; |
| | 100 | }; |
| | 101 | return '<p>'.implode($a).'</p>'; |
| | 102 | }; |
| | 103 | |
| | 104 | // untested |
| | 105 | function check_and_set($item, $assoc_array) { |
| | 106 | if (array_key_exists($item, $assoc_array)) { |
| | 107 | $val = $assoc_array[$item]; |
| | 108 | } else { |
| | 109 | $val = ' '; |
| | 110 | }; |
| | 111 | return $val; |
| | 112 | }; |
| | 113 | |
| | 114 | function process_hospital_info($hospital) { |
| | 115 | global $hospital_table_format; |
| | 116 | |
| | 117 | $city = check_and_set('city', $hospital); |
| | 118 | $website = check_and_set('website', $hospital); |
| | 119 | $name = check_and_set('name', $hospital); |
| | 120 | $address = check_and_set('address', $hospital); |
| | 121 | $phone = check_and_set('phone', $hospital); |
| | 122 | $email = check_and_set('email', $hospital); |
| | 123 | $lat = check_and_set('@lat', $hospital['$k_location_id']); |
| | 124 | $lon = check_and_set('@lon', $hospital['$k_location_id']); |
| | 125 | $comments = check_and_set('comments', $hospital); |
| | 126 | $modified_by = check_and_set('@modified_by', $hospital); |
| | 127 | $last_modified = check_and_set('@modified_on', $hospital); |
| | 128 | $uuid = check_and_set('@uuid', $hospital); |
| | 129 | |
| | 130 | return sprintf($hospital_table_format, $website, $name, $address, $city, $phone, $website, $email, $lat, $lon, $comments, $modified_by, $last_modified, $uuid); |
| | 131 | }; |
| | 132 | |
| | 133 | |
| | 134 | |
| | 135 | echo $head; |
| | 136 | echo '<h3>yo!</h3>'; |
| | 137 | |
| | 138 | $hospital_json = file_get_contents($hospital_source_file); |
| | 139 | if ($hospital_json === False) { |
| | 140 | $download_error = True; |
| | 141 | } else { |
| | 142 | $hospitals = json_decode($hospital_json, True); |
| | 143 | }; |
| | 144 | |
| | 145 | foreach ($hospitals['$_hms_hospital'] as &$hospital) { |
| | 146 | $hospital_table[] = process_hospital_info($hospital); |
| | 147 | } |
| | 148 | print '<table border="0" style="padding:3px;">'; |
| | 149 | print implode($hospital_table); |
| | 150 | print '</table>'; |
| | 151 | |
| | 152 | echo '</body>'; |
| | 153 | ?> |
| | 154 | }}} |
| | 155 | |
| | 156 | == Testing == |
| | 157 | === RESTclient === |
| | 158 | * http://code.google.com/p/rest-client/ |
| | 159 | |
| | 160 | === CURL === |