Laravel的卷曲错误18错误

问题描述:

我有一个api和一个正在开发的客户端,都在laravel中,当我尝试使用guzzle连接到api时,出现错误18。Laravel的卷曲错误18错误

在我的API控制器我有这样的:

public function index() 
{ 
    $users = User::orderBy('username', 'asc'); 

    return Response::json(array(
     'error' => false, 
     'users' => $users->get()->toArray()), 
     200 
    ); 

} 

而且,如果我做卷曲--user [email protected]:密码http://myapi.api/api/v1/users

我得到一些正确需要我的控制台上的信息:

{"error":false,"users":[{"id":"1","firstname":"","lastname":"","username":"[email protected]","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甚至还可以在浏览器(我得到一个弹出身份验证后我得到它的浏览器相同的输入)。所以这只是通过炫耀失败。

现在我的客户对我已经狂饮安装,我想这样的:

$client = new GuzzleHttp\Client(); 
$user='[email protected]'; 
$pass='password'; 

$res = $client->get('http://myapi.api/api/v1/users', array(
    'auth' => array('[email protected]', '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 [email protected]: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 '[email protected]' 
> 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":"[email protected]","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"} 

编辑:这个问题可能是过于具体,即使我还没有找到一个解决方案,并已走了不同的方式与这个项目,因为规格已经改变我会把它留在这里,以防它可以帮助某人。

我对答案的猜测是安德烈丹尼尔在评论中建议,认证正在破坏一些东西,并且不会给我我原来的json,从而产生错误。

+0

错误#18表示* CURLE_PARTIAL_FILE - 文件传输短于或大于预期。当服务器首次报告预期的传输大小,然后传递与先前给定大小*不匹配的数据时,会发生这种情况。 – 2014-10-19 21:33:05

+0

我更新了我的问题,André。我该如何解决它?我没有用curl工作太多,但是这个代码在一个非常类似的项目上工作。 – 2014-10-20 07:48:34

+0

使用该信息再次编辑 – 2014-10-20 13:09:59

在你的问题你说的这个URL通过命令行工作:

http://myapi.api/api/v1/users/1 

但你调用这个URL通过狂饮

http://myapi.api/api/v1/users 

那么,你确定你想打电话路线通过guzzle通过命令行工作?

+0

这两个网址应该工作,一个给一个用户,另一个给所有 – 2014-10-20 10:23:00

+0

是的 - 我知道 - 但* *确实*两个网址通过控制台工作?您没有比较相同的网址,因此我们无法确定问题是否与枪口或代码相关。请修改问题以确认'http:// myapi失败。api/api/v1/users/1'和它给出的错误*那个* url – Laurence 2014-10-20 10:25:27

+0

是的,它们都在控制台上工作,它只是一个错字,当复制时发出问题 – 2014-10-20 10:40:30

我终于改变了方法,因为我们需要一种不同的身份验证方法,所以这个问题与我的情况不再相关。

+0

不是一个正确的答案。如果试图让你的赏金回来,它[不会工作](http://meta.*.com/a/275184/1836314)。 – matpop 2014-10-24 08:46:13

+0

我不需要赏金。没有其他适当的答案给予,所以没有人的赏金。 – 2014-10-24 09:03:33

+0

未来可能会有适当的答案。如果您认为某个问题只与您自己相关,最好不要将它发布在SO上。 NRN bye – matpop 2014-10-24 09:11:42