我有一個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,從而產生錯誤。
錯誤#18表示* CURLE_PARTIAL_FILE - 文件傳輸短於或大於預期。當服務器首次報告預期的傳輸大小,然後傳遞與先前給定大小*不匹配的數據時,會發生這種情況。 – 2014-10-19 21:33:05
我更新了我的問題,André。我該如何解決它?我沒有用curl工作太多,但是這個代碼在一個非常類似的項目上工作。 – 2014-10-20 07:48:34
使用該信息再次編輯 – 2014-10-20 13:09:59