带有 PHP Curl 的 WordPress JSON API AUTH

雷神

我正在使用 wordpress 并使用此插件https://wordpress.org/plugins/json-api-auth/进行 JSON API 身份验证,我试图在 PHP 中使用 curl 返回成功,但是每当我运行代码时,我都会收到{"status":"error","error":"Invalid username and\/or password."}

<?php
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
?>

但是当我把它放在浏览器网址中时,我就https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd成功了{"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"[email protected]","url":"","registered":"2020-09-10 10:42:41","displayname":"abcd","firstname":"abcd","lastname":"abcd","nickname":"abcd","description":"","capabilities":{"administrator":true},"avatar":null}}

你能帮我在 PHP 中使用 curl 做错什么吗?

我在代码中使用的 CURLOPT_URL 的原始版本

CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="'.$this->input->post('email').'"&password="'.$this->input->post('password').'"',
乔什·邦尼克

从 cURL url 查询字符串中删除 "。处理您的请求的 WordPress PHP 将读取您的用户名,"abcd"而不是abcd您试图实现的。

CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"'

应该

CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章