Laravel 4.2尝试使用post方法授权用户

时间:2016-02-08 11:39:31

标签: php ios json laravel-4

我正在尝试发帖请求授权用户。

这是我的route.php

Route::post('/user/login', 'OwnerController@login');

这是我的登录方法:

$login_by = Input::get('login_by');
    $device_token = Input::get('device_token');
    $device_type = Input::get('device_type');

    if (Input::has('email') && Input::has('password')) {
        $email = Input::get('email');
        $password = Input::get('password');
        $validator = Validator::make(
                        array(
                    'password' => $password,
                    'email' => $email,
                    'device_token' => $device_token,
                    'device_type' => $device_type,
                    'login_by' => $login_by
                        ), array(
                    'password' => 'required',
                    'email' => 'required|email',
                    'device_token' => 'required',
                    'device_type' => 'required|in:android,ios',
                    'login_by' => 'required|in:manual,facebook,google'
                        )
        );

        if ($validator->fails()) {
            $error_messages = $validator->messages()->all();
            $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages);
            $response_code = 200;
            Log::error('Validation error during manual login for owner = ' . print_r($error_messages, true));
        } else {
            if ($owner = Owner::where('email', '=', $email)->first()) {
                if (Hash::check($password, $owner->password)) {
                    if ($login_by !== "manual") {
                        $response_array = array('success' => false, 'error' => 'Login by mismatch', 'error_code' => 417);
                        $response_code = 200;
                    } else {
                        if ($owner->device_type != $device_type) {
                            $owner->device_type = $device_type;
                        }
                        if ($owner->device_token != $device_token) {
                            $owner->device_token = $device_token;
                        }
                        $owner->token = generate_token();
                        $owner->token_expiry = generate_expiry();
                        $owner->save();
                        /* SEND REFERRAL & PROMO INFO */
                        $settings = Settings::where('key', 'referral_code_activation')->first();
                        $referral_code_activation = $settings->value;
                        if ($referral_code_activation) {
                            $referral_code_activation_txt = "referral on";
                        } else {
                            $referral_code_activation_txt = "referral off";
                        }

                        $settings = Settings::where('key', 'promotional_code_activation')->first();
                        $promotional_code_activation = $settings->value;
                        if ($promotional_code_activation) {
                            $promotional_code_activation_txt = "promo on";
                        } else {
                            $promotional_code_activation_txt = "promo off";
                        }
                        /* SEND REFERRAL & PROMO INFO */
                        $code_data = Ledger::where('owner_id', '=', $owner->id)->first();

                        $response_array = array(
                            'success' => true,
                            'id' => $owner->id,
                            'first_name' => $owner->first_name,
                            'last_name' => $owner->last_name,
                            'phone' => $owner->phone,
                            'email' => $owner->email,
                            'picture' => $owner->picture,
                            'bio' => $owner->bio,
                            'address' => $owner->address,
                            'state' => $owner->state,
                            'country' => $owner->country,
                            'zipcode' => $owner->zipcode,
                            'login_by' => $owner->login_by,
                            'social_unique_id' => $owner->social_unique_id,
                            'device_token' => $owner->device_token,
                            'device_type' => $owner->device_type,
                            'timezone' => $owner->timezone,
                            'token' => $owner->token,
                            'referral_code' => $code_data->referral_code,
                            'is_referee' => $owner->is_referee,
                            'promo_count' => $owner->promo_count,
                            'is_referral_active' => $referral_code_activation,
                            'is_referral_active_txt' => $referral_code_activation_txt,
                            'is_promo_active' => $promotional_code_activation,
                            'is_promo_active_txt' => $promotional_code_activation_txt,
                        );

                        $dog = Dog::find($owner->dog_id);
                        if ($dog !== NULL) {
                            $response_array = array_merge($response_array, array(
                                'dog_id' => $dog->id,
                                'age' => $dog->age,
                                'name' => $dog->name,
                                'breed' => $dog->breed,
                                'likes' => $dog->likes,
                                'image_url' => $dog->image_url,
                            ));
                        }

                        $response_code = 200;
                    }
                } else {
                    $response_array = array('success' => false, 'error' => 'Invalid Username and Password', 'error_code' => 403);
                    $response_code = 200;
                }
            } else {
                $response_array = array('success' => false, 'error' => 'Not a Registered User', 'error_code' => 404);
                $response_code = 200;
            }
        }
         return Response::make(json_encode($response_array,JSON_PRETTY_PRINT))->header('Content-Type', "application/json");

但是当我尝试使用AFNETWORKING从我的ios项目访问此网址时:Login Link Localhost

我收到了这个错误:

  

网址:http://192.168.1.26/uberx/public/user/login} {状态代码:   500,标题{       "缓存控制" =" no-cache&#34 ;;       连接=关闭;       " Content-Length的" = 4390;       "内容类型" =" text / html;字符集= UTF-8&#34 ;;       Date =" Mon,08 Feb 2016 09:04:56 GMT&#34 ;;       Server =" Apache / 2.4.17(Win32)OpenSSL / 1.0.2d PHP / 5.6.14&#34 ;;       "的Set-Cookie" =" laravel_session = eyJpdiI6Iis2VnVUN2JWQW9uMEFkcTBkUkJtNWc9PSIsInZhbHVlIjoiVVpMeW1KbkRMblA2K1hSNGpBSHpHaHNhNGt2a3hab1BaNmdPVVQ3NDVlaFBOZnFUM3QrdjR3dWlQUFwvTFZIa3VkT0ZUcldiYUxqNmE1QXNlT3hjRjB3PT0iLCJtYWMiOiIwOTJjZjlkMWVlZjg5NzQ3ZmY3MDA1YjBlYjdhN2U4MmE3M2I5YzUwMDU0Y2E4ZmFlMTkyNzVkZWI2ZDI0MTBmIn0%3D;   expires =周一,08-Feb-2016 11:04:57 GMT;最大年龄= 7200;路径= /;   仅Http&#34 ;;       " X供电-通过" =" PHP / 5.6.14&#34 ;; ,NSLocalizedDescription =请求失败:不可接受的内容类型:text / html}}} 2016-02-08   14:34:43.948 Upper [23084:6636866]登录答案---> (空)

是否有任何特定的方法来创建类似方法和打印json的Web服务? IOS调用或Web输出中是否存在问题?

由于

0 个答案:

没有答案