本文整理汇总了PHP中WP_REST_Server::send_header方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Server::send_header方法的具体用法?PHP WP_REST_Server::send_header怎么用?PHP WP_REST_Server::send_header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Server
的用法示例。
在下文中一共展示了WP_REST_Server::send_header方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _oembed_rest_pre_serve_request
/**
* 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;
}
示例2: rest_pre_serve_request
/**
* Hooks into the REST API output to print XML instead of JSON.
*
* @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 bool
*/
public function rest_pre_serve_request($served, $result, $request, $server)
{
$params = $request->get_params();
if ('/wp/v2/oembed' !== $request->get_route() || 'xml' !== $params['format']) {
return $served;
}
if ('HEAD' === $request->get_method()) {
return $served;
}
if (!headers_sent()) {
$server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset'));
}
// Embed links inside the request.
$result = $server->response_to_data($result, false);
$oembed = new SimpleXMLElement('<oembed></oembed>');
foreach ($result as $key => $value) {
if (is_array($value)) {
$element = $oembed->addChild($key);
foreach ($value as $k => $v) {
$element->addChild($k, $v);
}
continue;
}
$oembed->addChild($key, $value);
}
echo $oembed->asXML();
return true;
}
示例3: _oembed_rest_pre_serve_request
/**
* Hooks into the REST API output to print XML instead of JSON.
*
* @access private
*
* @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 ('/wp/v2/oembed' !== $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];
}
/**
* Filter the XML response.
*
* @param array $data The original oEmbed response data.
*/
$result = apply_filters('oembed_xml_response', $data);
// Bail if there's no XML.
if (!is_string($result)) {
status_header(501);
die('Not implemented');
}
if (!headers_sent()) {
$server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset'));
}
echo $result;
return true;
}
示例4: oembed_xml_request
/**
* If oEmbed request wants XML, return XML instead of JSON.
*
* Basically a copy of {@link _oembed_rest_pre_serve_request()}. Unfortunate
* that we have to duplicate this just for a URL check.
*
* @since 2.6.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 bool
*/
public function oembed_xml_request($served, $result, $request, $server)
{
$params = $request->get_params();
if (!isset($params['format']) || 'xml' !== $params['format']) {
return $served;
}
// Validate URL against our oEmbed endpoint. If not valid, bail.
// This is our mod to _oembed_rest_pre_serve_request().
$query_params = $request->get_query_params();
if (false === $this->validate_url_to_item_id($query_params['url'])) {
return $served;
}
// Embed links inside the request.
$data = $server->response_to_data($result, false);
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;
}