我有一个api和一个我正在开发的客户端,无论是在laravel还是当我尝试使用guzzle连接到api时我得到了错误18。
在我的api控制器中,我有这个:
public function index()
{
$users = User::orderBy('username', 'asc');
return Response::json(array(
'error' => false,
'users' => $users->get()->toArray()),
200
);
}
如果我做curl --user admin@admin.com:password http://myapi.api/api/v1/users
我在控制台上正确获取了我需要的信息:
{"error":false,"users":[{"id":"1","firstname":"","lastname":"","username":"admin@admin.com","created_at":"2014-10-17 15:35:10","updated_at":"2014-10-17 15:35:10","client_id":"0","enterprise_id":"0","usertype_id":"0"},{"id":"2","firstname":"","lastname":"","username":"seconduser","created_at":"2014-10-17 15:35:10","updated_at":"2014-10-17 15:35:10","client_id":"0","enterprise_id":"0","usertype_id":"0"}]}
网址甚至可以在浏览器中运行(我获得了一个用于身份验证的弹出窗口,之后我在浏览器上获得了相同的输入)。所以它只能通过guzzle来失败。
现在我的客户端安装了Guzzle,我正在尝试这个:
$client = new GuzzleHttp\Client();
$user='admin@admin.com';
$pass='password';
$res = $client->get('http://myapi.api/api/v1/users', array(
'auth' => array('admin@admin.com', 'password')
));
$users=$res->json();
$users=$users['users'];
我收到错误:
[curl] (#18) See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of cURL errors [url] http://myapi.api/api/v1/users
我做错了什么?
编辑:在我得到的命令中使用-v:
$ curl -v --user admin@admin.com:password http://myapi.api/api/v1/users
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to myapi.api (127.0.0.1) port 80 (#0)
* Server auth using Basic with user 'admin@admin.com'
> GET /api/v1/users HTTP/1.1
> Authorization: Basic YWRtaW5AYWRtaW4uY29tOnBhc3N3b3Jk
> User-Agent: curl/7.37.1
> Host: myapi.api
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Mon, 20 Oct 2014 13:08:56 GMT
* Server Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.5.17 mod_ssl/2.2.29 OpenSSL/0.9.8za DAV/2 mod_perl/2.0.8 Perl/v5.20.0 is not blacklisted
< Server: Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.5.17 mod_ssl/2.2.29 OpenSSL/0.9.8za DAV/2 mod_perl/2.0.8 Perl/v5.20.0
< X-Powered-By: PHP/5.5.17
< Cache-Control: no-cache
< Set-Cookie: laravel_session=eyJpdiI6IjRPMk9TT0ZnZklTMG1uWlFDancyMWc9PSIsInZhbHVlIjoic1V1RjA5aVBJdFNLM0JLclNROEE1a0dCeHNEMWhVNFVReTlUOHdidE44WEJzRnB4WFkxdWo0V0ozcXFVSW9LYzZiZzZSSlFCNXNTTjl2Mzh4TlFtTUE9PSIsIm1hYyI6IjhlYzY0YjFkNTQzNjk5ZGMxNDk3YmY4ZjU4YTYzYzM4YzgxZjg1MzlhMWUxNWVjYWE4ZThlMmU0N2RjNWFkZGMifQ%3D%3D; expires=Mon, 20-Oct-2014 15:08:57 GMT; Max-Age=7200; path=/; httponly
< Transfer-Encoding: chunked
< Content-Type: application/json
<
* Connection #0 to host myapi.api left intact
{"error":false,"users":[{"id":"1","firstname":"","lastname":"","username":"admin@admin.com","created_at":"2014-10-17 15:35:10","updated_at":"2014-10-17 15:35:10","client_id":"0","enterprise_id":"0","usertype_id":"0"},{"id":"2","firstname":"","lastname":"","username":"seconduser","created_at":"2014-10-17 15:35:10","updated_at":"2014-10-17 15:35:10","client_id":"0","enterprise_id":"0","usertype_id":"0"}
编辑:这个问题可能过于具体,即使我没有找到解决方案,并且已经采用了不同的方式,因为规格已经改变,我会留在这里,以防它可以帮助某人。
我对答案的猜测是正如AndréDaniel在评论中所说的那样,认证破坏了某些东西,而guzzle并没有给我原来的json,从而产生错误。
答案 0 :(得分:0)
关于你的问题,你说这个URL通过命令行工作:
http://myapi.api/api/v1/users/1
但是您通过guzzle
调用此URL http://myapi.api/api/v1/users
所以你确定你试图通过guzzle调用的路由通过命令行工作吗?
答案 1 :(得分:0)
我终于改变了方法,因为我们需要一种不同的身份验证方法,因此这个问题不再与我的案例相关。
答案 2 :(得分:0)
我知道这已经很老了,但这就是我与Guzzle一起工作的原因:
switch (button.tag) {
case != 1: //error occurs here
default:
//
}
您甚至可以将授权数据存储在Guzzle实例上,如下所示:
$client = new GuzzleHttp\Client();
$user = 'admin@admin.com';
$pass = 'password';
$res = $client->get('http://myapi.api/api/v1/users', [
'headers' => [
'Authorization' => 'Basic ' . base64_encode($user . ":" . $pass),
],
]);
$users = $res->json();
$users = $users['users'];