【问题标题】:WordPress JSON API AUTH with PHP Curl带有 PHP Curl 的 WordPress JSON API AUTH
【发布时间】:2021-02-02 17:53:36
【问题描述】:

我正在使用 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;
?>

但是当我把它放在浏览器 url https://example.com/api/auth/generate_auth_cookie/?username=abcd&amp;password=abcd 中时,我得到了成功 {"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"abcd@abcd.com","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').'"',

【问题讨论】:

    标签: php wordpress wordpress-rest-api


    【解决方案1】:

    从 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'
    

    【讨论】:

    • 我已经编辑了问题,您可以在问题底部查看吗?
    • @Thor 你的网址中仍然有不应该存在的双引号。
    • CURLOPT_URL =&gt; 'https://example.com/api/auth/generate_auth_cookie/?username=' . $this-&gt;input-&gt;post('email') . '&amp;password=' . $this-&gt;input-&gt;post('password'); 正如@Jonnix 上面所说,你仍然有我为你删除的那些双引号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    相关资源
    最近更新 更多