WP_REST_Server::send_header PHP Method

send_header() public method

Sends an HTTP header.
Since: 4.4.0
public send_header ( string $key, string $value )
$key string Header key.
$value string Header value.
    public function send_header($key, $value)
    {
        /*
         * Sanitize as per RFC2616 (Section 4.2):
         *
         * Any LWS that occurs between field-content MAY be replaced with a
         * single SP before interpreting the field value or forwarding the
         * message downstream.
         */
        $value = preg_replace('/\\s+/', ' ', $value);
        header(sprintf('%s: %s', $key, $value));
    }

Usage Example

示例#1
0
/**
 * Hooks into the REST API output to print XML instead of JSON.
 *
 * This is only done for the oEmbed API endpoint,
 * which supports both formats.
 *
 * @access private
 * @since 4.4.0
 *
 * @param bool                      $served  Whether the request has already been served.
 * @param WP_HTTP_ResponseInterface $result  Result to send to the client. Usually a WP_REST_Response.
 * @param WP_REST_Request           $request Request used to generate the response.
 * @param WP_REST_Server            $server  Server instance.
 * @return true
 */
function _oembed_rest_pre_serve_request($served, $result, $request, $server)
{
    $params = $request->get_params();
    if ('/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method()) {
        return $served;
    }
    if (!isset($params['format']) || 'xml' !== $params['format']) {
        return $served;
    }
    // Embed links inside the request.
    $data = $server->response_to_data($result, false);
    if (404 === $result->get_status()) {
        $data = $data[0];
    }
    if (!class_exists('SimpleXMLElement')) {
        status_header(501);
        die(get_status_header_desc(501));
    }
    $result = _oembed_create_xml($data);
    // Bail if there's no XML.
    if (!$result) {
        status_header(501);
        return get_status_header_desc(501);
    }
    if (!headers_sent()) {
        $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset'));
    }
    echo $result;
    return true;
}
All Usage Examples Of WP_REST_Server::send_header