首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >405从WordPress调用接口错误

405从WordPress调用接口错误
EN

Stack Overflow用户
提问于 2021-05-01 06:22:48
回答 1查看 31关注 0票数 0

我需要使用访问令牌和主体从API获取一些数据,因此我进行了以下操作(随机化了密钥和ids):

代码语言:javascript
复制
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代码哪里出了问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-01 17:29:36

我意识到我使用的是wp_remote_post而不是wp_remote_request (所以使用POST而不只是GET)。一旦我这样做了,它就工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67341095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档