【发布时间】: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&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