/** * Output and return API result * * This function will echo (or only return if asked) an array as JSON, JSONP or XML. If the array has a * 'simple' key, it can also output that key as unformatted text if expected output mode is 'simple' * * Most likely, script should not do anything after outputting this * * @since 1.6 * * @param string $mode Expected output mode ('json', 'jsonp', 'xml', 'simple') * @param array $output Array of things to output * @param bool $send_headers Optional, default true: Whether a headers (status, content type) should be sent or not * @param bool $echo Optional, default true: Whether the output should be outputted or just returned * @return string API output, as an XML / JSON / JSONP / raw text string */ function yourls_api_output($mode, $output, $send_headers = true, $echo = true) { if (isset($output['simple'])) { $simple = $output['simple']; unset($output['simple']); } yourls_do_action('pre_api_output', $mode, $output, $send_headers, $echo); if ($send_headers) { if (isset($output['statusCode'])) { $code = $output['statusCode']; } elseif (isset($output['errorCode'])) { $code = $output['errorCode']; } else { $code = 200; } yourls_status_header($code); } $result = ''; switch ($mode) { case 'jsonp': if ($send_headers) { yourls_content_type_header('application/javascript'); } $callback = isset($output['callback']) ? $output['callback'] : ''; $result = $callback . '(' . json_encode($output) . ')'; break; case 'json': if ($send_headers) { yourls_content_type_header('application/json'); } $result = json_encode($output); break; case 'xml': if ($send_headers) { yourls_content_type_header('application/xml'); } $result = yourls_xml_encode($output); break; case 'simple': default: if ($send_headers) { yourls_content_type_header('text/plain'); } $result = isset($simple) ? $simple : ''; break; } if ($echo) { echo $result; } yourls_do_action('api_output', $mode, $output, $send_headers, $echo); return $result; }
/** * Return API result. Dies after this * */ function yourls_api_output($mode, $return) { if (isset($return['simple'])) { $simple = $return['simple']; unset($return['simple']); } yourls_do_action('pre_api_output', $mode, $return); if (isset($return['statusCode'])) { $code = $return['statusCode']; } elseif (isset($return['errorCode'])) { $code = $return['errorCode']; } else { $code = 200; } yourls_status_header($code); switch ($mode) { case 'jsonp': yourls_content_type_header('application/javascript'); echo $return['callback'] . '(' . json_encode($return) . ')'; break; case 'json': yourls_content_type_header('application/json'); echo json_encode($return); break; case 'xml': yourls_content_type_header('application/xml'); echo yourls_xml_encode($return); break; case 'simple': default: yourls_content_type_header('text/plain'); if (isset($simple)) { echo $simple; } break; } yourls_do_action('api_output', $mode, $return); die; }
function yourls_api_output($mode, $return) { if (isset($return['simple'])) { $simple = $return['simple']; unset($return['simple']); } switch ($mode) { case 'json': header('Content-type: application/json'); echo yourls_json_encode($return); break; case 'xml': header('Content-type: application/xml'); echo yourls_xml_encode($return); break; case 'simple': default: if (isset($simple)) { echo $simple; } break; } die; }