当您可以访问这两个站点时。您可以创建一个数据馈送url,该url将返回JSON
数据
提供数据馈送的插件(安装在站点A上)
<?php
/*
Plugin Name: WPSE Search Data Feed
Plugin URI: http://wordpress.stackexchange.com
Description: Provides interface to search and get result in JSON data
Author: Sisir
Version: 1.0
Author URI: http://developerpage.net
*/
add_action(\'wp_ajax_nopriv_wpse144893_search\', \'wpse144893_search_data\'); // allow logged out users
add_action(\'wp_ajax_wpse144893_search\', \'wpse144893_search_data\'); // allow logged in users
function wpse144893_search_data(){
$errors = array();
$data = array(
\'status\' => \'error\',
\'message\' => \'\',
\'result\' => array()
);
if(!isset($_REQUEST[\'term\']) || empty($_REQUEST[\'term\']))
$errors[] = \'No search term given!\';
if(!isset($_REQUEST[\'limit\']) || empty($_REQUEST[\'limit\']))
$limit = 10;
else
$limit = (int) $_REQUEST[\'limit\'];
if(empty($errors)){
$term = sanitize_text_field($_REQUEST[\'term\']);
// setup query data
$args = array(
\'posts_per_page\' => $limit,
\'s\' => $term
);
$query = new WP_Query($args); // run query
$results = array();
if($query->have_posts()): while($query->have_posts()): $query->the_post();
$post_item = array(
\'title\' => get_the_title(),
\'excerpt\' => get_the_excerpt(),
\'permalink\' => get_permalink()
);
$results[] = $post_item;
endwhile;
$data[\'status\'] = \'success\';
$data[\'message\'] = \'Results found!\';
$data[\'result\'] = $results;
else:
$errors[] = \'No post found!\';
$data[\'message\'] = $errors;
endif;
}
echo json_encode($data); // print json
die(); // kill the script
}
Link to Gist
返回的结果如下:
{
"status":"success",
"message":"Results found!",
"result":[
{
"title":"Deadly car bomb blasts hit Shia areas in Baghdad",
"excerpt":"Several car bombs have exploded across the Iraqi capital Baghdad, killing at least 20 people and injuring scores more, officials say. The attacks are reported to have targeted mainly Shia areas in the city. Security sources told the BBC three explosions rocked the neighbourhoods of Ourfally, Kiyara and Falah Street in the eastern suburb of […]",
"permalink":"http:\\/\\/localhost\\/l\\/blog\\/deadly-car-bomb-blasts-hit-shia-areas-in-baghdad\\/"}
]
}
解释我们使用ajax api创建一个数据提要。无需致电
admin-ajax.php
使用ajax的文件。我们可以使用简单的HTTP\\U API来实现这一点或CURL。我们获取搜索结果的url端点是
http://<your_domain.com>/wp-admin/admin-ajax.php?action=wpse144893_search
从验证代码中可以看到。您将搜索词传递为term
钥匙你也可以通过limit
不需要,默认为10
. 以下是搜索词的外观。
http://<your_domain.com>/wp-admin/admin-ajax.php?action=wpse144893_search&term=<my_search>&limit=50
您只需将url粘贴到浏览器上即可查看结果。这是一个给你一个想法的基本插件。
通过重写api,可以对插件的自定义/漂亮url端点进行改进通过密钥保护端点支持XML。我对JSON
但你可以回来XML
如果你觉得舒服的话如何从站点B调用,正如您所看到的,url非常简单。因此,您可以使用HTTP_API
或php curl来获得结果。下面是如何使用HTTP_API
$url = \'http://<your_domain.com>/wp-admin/admin-ajax.php\';
$args = array(
\'action\' => \'wpse144893_search\',
\'term\' => \'My Search Term\',
\'limit\' => 50
);
$url = add_query_arg($args, $url);
$response = wp_remote_get($url);
if(!is_wp_error($response)){
// additional checks can be done here for correct http code.
$body = wp_remote_retrieve_body($response); // retrieve the response body
$result = json_decode($result); // now we get the result object :)
}else{
// handle error
}
现在您可以处理$result
对象以获取搜索结果。