英文:
Issue with Creating Custom REST Endpoint
问题
你好,我正在尝试创建一个新的REST API端点,该端点连接到我的数据库,使用mysql查询,然后返回学校的ID号码。但是,端点未注册,并且我收到了WordPress的错误消息,其中包含以下内容:在激活期间生成了3个意外输出字符的插件。如果您注意到“已发送标头”消息、订阅源问题或其他问题,请尝试停用或移除此插件。
代码在我看来是正确的,所以我不确定为什么会出现这个错误。
最终结果我只是想能够使用命名空间和自定义端点访问数据。任何帮助都将是巨大的帮助,因为我对问题感到困惑。当访问链接时的输出是:“路由的处理程序无效。”错误500
英文:
Hello I am trying to create a new rest API endpoint that connects to my database querying with mysql and then returning the id number from the school. However, the endpoint is not registering and I am getting an error from wordpress that states the following: The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
The code looks okay to me so I am unsure to exactly why I am getting this error.
<?php
/*
Plugin Name: College ID API
* Requires PHP: 7.2
*/
include connect.php
function collegeid()
{
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
$queryusers = "SELECT 'id' FROM `Colleges` ";
$dbc = mysqli_query($db, $queryusers);
return $dbc;
};
add_action( 'rest_api_init', function () {
register_rest_route( 'schfind/v2', '/schools/', array(
'methods' => 'GET',
'callback' => ['collegeid'],
'permission_callback'=>'__return_true'
) );
} );
?>
For the final result I am just trying to be able to access the data using the namespace and custom endpoint. It would be a huge help any help at all really because I am stumped as to the issue. The output when visiting the link is:
"The handler for the route is invalid." Error 500
答案1
得分: 0
这是对你的问题的答复。在我的答案中,我对包含部分进行了一些更改,还注释掉了返回语句以使你的函数正常工作。
include "connect.php";
function collegeid()
{
//Comment the below line to work on your query.
return 'Howdy!!!';
//Comment the above line to make your function work.
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
$queryusers = "SELECT 'id' FROM `Colleges` ";
$dbc = mysqli_query($db, $queryusers);
return $dbc;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'schfind/v2', '/schools/', array(
'methods' => 'GET',
'callback' => 'collegeid',
'permission_callback'=>'__return_true'
) );
});
完整插件链接的GitHub链接:https://github.com/SHABU89/wordpress_rest_api_help_in_stackoverfolw
英文:
Here is the answer for your question. In my answer I have made some changes with the include and also comment out the return to make your function work.
include "connect.php";
function collegeid()
{
//Comment the below line to work on your query.
return 'Howdy!!!';
//Comment the above line to make your function work.
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
$queryusers = "SELECT 'id' FROM `Colleges` ";
$dbc = mysqli_query($db, $queryusers);
return $dbc;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'schfind/v2', '/schools/', array(
'methods' => 'GET',
'callback' => 'collegeid',
'permission_callback'=>'__return_true'
) );
} );
Github link for full plugin link : https://github.com/SHABU89/wordpress_rest_api_help_in_stackoverfolw
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论