mercredi 12 décembre 2018

Laravel Rest Api Redirect to login page

I'm Working on Native Rest Api on Laravel I have API's in controller directory folder named api and inside of that folder their is Controllers\Api\Message\MessageApi

Now problem is that when is hit api with curl helper i'm not getting response json() but instead i get HTML Content show in below image enter image description here

Below is my curl code

                  public static function setupCurl($data, $url, $image = null, $options) {
                      $token_secret = Config('constants.secret');

                      if (empty($token_secret)) {
                              $return_data['is_token_error'] = true;
                              return $return_data;
                      } else {
                              $data['token'] = $token_secret;
                              $data['type'] = 'WEB';

                              $data_e = array(
                                        'data' => json_encode($data)
                              );
                              if ($image != NULL) {
                                      $data_e = array_merge($data_e, $image);
                              }

                              $curl = curl_init();

                              if (isset($options['curl_method']) == 'GET') {
                                      $uri_string = '';

                                      if (isset($data['params'])) {
                                              foreach ($data['params'] as $uri_data) {
                                                      $uri_string = $uri_data . '/';
                                              }
                                              curl_setopt($curl, CURLOPT_URL, $url . '/' . substr(trim($uri_string), 0, -1));
                                      }
                              } else {
                                      curl_setopt($curl, CURLOPT_URL, $url);
                                      curl_setopt($curl, CURLOPT_POST, 1);
                                      curl_setopt($curl, CURLOPT_POSTFIELDS, $data_e);
                              }
                              curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

                              $result = curl_exec($curl);
                              curl_close($curl);
                              return $result;
                      }
              }

Here is my route.php

  Route::group(['middleware' => ['api.token.auth'], 'prefix' => 'api/'], function () {

      $prepend_controller_name = "Api\Message\MessageApi@";
      Route::post('message/remove/file', $prepend_controller_name . 'removeFile');

});

Here is Middleware : ApiTokenAuth

    class ApiTokenAuth {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $requested_data = json_decode($request->data, TRUE);

        if ($request->method() != "GET" && $requested_data['token'] != Config('constants.secret')) {
            $info_array = [
                'error' => [
                    'ERROR_IDENTIFICATION' => 'TOKEN_MISMATCH',
                    'ERROR_DESCRIPTION' => 'Key does not match.',
                ]
            ];

            return response()->json($info_array);
        }

        return $next($request);
    }

}

below is the Business logic code in Api/Message/MessageApi

public function removeFile(Request $request) {
                      $messageFileModel = new MessageFileModel();
                      $messageFileModel_table_name = $messageFileModel->getTable();

                      Log::info(json_encode($this->requested_data));

                      $info_array = [
                                'success' => FALSE,
                                'report' => 'Something went wrong.',
                                'result' => $messageFileModel->updateData([
                                          'condition' => [
                                                    'message_file_id' => $this->requested_data['message_file_id']
                                          ],
                                          'update_data' => [
                                                    'is_deleted' => 0
                                          ]
                                ])
                      ];

                      if ($info_array['result'] == 1) {
                              $info_array['success'] = TRUE;
                              $info_array['report'] = 'Successful.';
                      }

                      return $info_array;
              }

I KNOW THEIR IS A WHOLE LOT OF INFO I HAVE PROVIDED BUT IT IS NECESSARY

THANKS IN ADVANCE##



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire