【发布时间】:2018-08-22 04:10:53
【问题描述】:
我在 laravel 中有一个 api 和一个正在开发的客户端,当我尝试使用 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"}]}
该 url 甚至可以在浏览器中使用(我得到一个用于身份验证的弹出窗口,之后我在浏览器上得到相同的输入)。所以它只是通过 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 从而产生错误。
【问题讨论】:
-
错误 #18 表示 CURLE_PARTIAL_FILE - 文件传输比预期的短或大。当服务器首先报告预期的传输大小,然后交付与先前给定大小不匹配的数据时,就会发生这种情况。
-
我更新了我的问题,安德烈。我该如何解决?我对 curl 的工作不多,但这段代码在一个非常相似的项目上工作。
-
使用该信息再次编辑
-
您可以尝试不进行基本身份验证吗?如果还是不行,你可以试试this version(我曾经在我这边测试过,效果很好),看看你那边是否可行? (当然要根据您的应用调整 URL)