我寫了這段代碼來注冊 REST 路由,它在瀏覽器中作業正常,但不適用于 CURL
function my_func()
{
return new WP_REST_Response('ok');
}
function init_my_func(){
$namespace = 'test/v1';
$route = 'fire';
register_rest_route($namespace, $route, array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'my_func',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
}
add_action('rest_api_init', 'init_my_func');
卷曲代碼:
$params = array();
$params['arg1'] = 'value1';
$json = json_encode($params);
$url = "http://example.com/wp-json/test/v1/fire";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Content-Type: application/json;"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
回應:
{\"code\":\"rest_no_route\",\"message\":\"No route was found matching the URL and request method.\",\"data\":{\"status\":404}}
uj5u.com熱心網友回復:
您通過 注冊了一個GET路由'methods' => WP_REST_Server::READABLE,但POST在 curl 中發送了一個請求。您需要統一請求方法,無論是GET還是POST。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345941.html
標籤:WordPress的
