我需要使用访问令牌和主体从API获取一些数据,因此我进行了以下操作(随机化了密钥和ids):
add_action('rest_api_init', 'wp_rest_user_endpoints');
function wp_rest_user_endpoints($request) {
register_rest_route('wp/v2', 'users/register', array(
'methods' => 'POST',
'callback' => 'wc_rest_user_endpoint_handler',
));
}
function wc_rest_user_endpoint_handler($request = null) {
$response = array();
$parameters = $request->get_json_params();
$userid = sanitize_text_field($parameters['data[attendeeid]']);
$body = array(
'accountid' => '121209102910',
'key' => 'asjkajskauweiajksakajsakjsaks',
);
$args = array(
'body' => $body,
);
$response = wp_remote_post( 'https://api-na.eventscloud.com/api/v2/global/authorize.json', $args );
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
$accesstoken = $data->accesstoken;
if($accesstoken ) {
$body = array(
'accesstoken' => $accesstoken,
'eventid' => '29102901920129',
'attendeeid' => '1212121902910920',
);
$args = array(
'body' => $body,
);
$response = wp_remote_post( 'https://api-na.eventscloud.com/api/v2/ereg/getAttendee.json', $args );
//Do something with this
}
}然而,我遇到的问题是,当我获得accesstoken,然后go向go和user发出请求时,我会从API得到一个405错误消息。我已经尝试通过Postman手动发出相同的请求,并且请求起作用了,那么是不是我的WordPress代码哪里出了问题?
发布于 2021-05-01 17:29:36
我意识到我使用的是wp_remote_post而不是wp_remote_request (所以使用POST而不只是GET)。一旦我这样做了,它就工作得很好。
https://stackoverflow.com/questions/67341095
复制相似问题