对Wordpress WP-API的外部请求 - 基本身份验证

时间:2015-07-30 13:11:16

标签: wordpress basic-authentication guzzle nonce wp-api

我正在尝试使用来自我的中间件(Laravel)的Guzzle(http工具)使用Basic Auth命中我的Wordpress API。

$username = 'myAdminUserName';
$password = 'myAdminPassword';
$uri = 'https://example.com/wp-json/mysite-api/cleared-action';
$response = $this->guzzle->put(
    $uri,
    [
        'headers' => [
            'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password )
        ],
        'body' => [
            'user_id' => $wordpressId  //passed into this function
        ]
    ]
);

然后点击我在Wordpress API中设置的路线

$routes['/mysite-api/cleared-action'] = array(
        array(array($this, 'automatedClearing'), WP_JSON_Server::ACCEPT_JSON 
                                               | WP_JSON_Server::CREATABLE 
                                               | WP_JSON_Server::EDITABLE)
    );

然而,就目前而言。它没有点击我的automatedClearing端点,看起来像这样

public function automatedClearing() {
    global $container;

    \Groups_User_Group::create( array('user_id' => 2903, 'group_id' => 13));

    $mySiteServices = $container['services'];

    $this->$mySiteServices->sendClearedEmail(2903);  //2903 = user_id
} 

我已经为用户ID使用了硬编码值。

我的呼叫始终收到200响应,因此肯定会遇到路由,但不会执行端点。答案基本上只是空洞的。

我的Wordpress access.log显示被击中的路线,但我的error.log没有显示任何内容。顺便说一句,这是一个laravel Homestead(流浪者)盒子打一个Wordpress流浪盒。

我想知道这是因为WP-API需要一个nonce?但我认为仅在Wordpress中需要nonce,而这是一个外部应用程序命中Wordpress。

我非常坚持这一点。非常感谢任何指导

1 个答案:

答案 0 :(得分:0)

尝试使用postman进行测试...如果这可以通过邮递员工作,那么你就会遇到laravel或guzzle的问题