如何制作自定义的WordPress REST API并获得阿拉伯语语言参数的帖子结果

发布于 2025-02-07 18:44:04 字数 661 浏览 2 评论 0原文

我创建API路由搜索 我尝试在阿拉伯语中获取一个参数搜索(两个单词) 但这没用,我几乎尝试了视频,但不起作用 尝试Unicode并编码JSON也

有任何解决方案吗?

我的API路由

    add_action('rest_api_init', function() {
    register_rest_route( 'wm/v1', '/search/(?p<request>.+)', array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'wm_search_post',
    ) );
    enter code here

和端点回调功能

function wm_search_post(WP_REST_Request $request){
    $req=JSON_encode($request['request']);
    
    return $req; 
     // here I try to test the results but get rest_no_route 
     //404 >>> but it is work if I remove parameter
}}

I create API route for search
I try to get a parameter search in Arabic containing about (two words)
but it didn't work, I try almost of video, and not working
try Unicode and encode JSON also

Does anybody have a solution for it?

my API route

    add_action('rest_api_init', function() {
    register_rest_route( 'wm/v1', '/search/(?p<request>.+)', array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'wm_search_post',
    ) );
    enter code here

and the Endpoint callback function

function wm_search_post(WP_REST_Request $request){
    $req=JSON_encode($request['request']);
    
    return $req; 
     // here I try to test the results but get rest_no_route 
     //404 >>> but it is work if I remove parameter
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2025-02-14 18:44:04

您的正则表达式不正确,也缺少一些闭合括号。让我们看看以下代码,它在我的末端工作。

add_action('rest_api_init', function() {
    register_rest_route( 'wm/v1', '/search/(?P<request>\w+)/', array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'wm_search_post',
    ));
});

function wm_search_post(WP_REST_Request $request) {
    $req= $request['request'];
    return rest_ensure_response($req); 
}

您可以得到响应http:// localhost/elementsKit/wp-json/wm/v1/search/&lt; pass_string_here&gt;

希望那会起作用!

Your regex expression is not correct and also some of your closing braces missing. Let's see the below code, it's working on my end.

add_action('rest_api_init', function() {
    register_rest_route( 'wm/v1', '/search/(?P<request>\w+)/', array(
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'wm_search_post',
    ));
});

function wm_search_post(WP_REST_Request $request) {
    $req= $request['request'];
    return rest_ensure_response($req); 
}

You can get a response http://localhost/elementskit/wp-json/wm/v1/search/<pass_string_here>

Hope that will work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文