Home > Blockchain >  Correct way to view data from API in laravel
Correct way to view data from API in laravel

Time:04-15

I am calling an API from external source and want to do the registration based on given API. I have few problem:

I would like to get the data and pass it to my view registration, but I am getting Undefined index:country. i know where I did wrong but I couldnt find the solution. in this method, I should declare my $countries right?

public function showRegistrationForm()
    {
        return view('auth.register');
        
    }

but I declared at my register(Request $request) method

public function register(Request $request)
    {
        $country=$request->input('country');
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'test',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{
            "country": "'.$country.'"
        }',
        CURLOPT_HTTPHEADER => array(
            'accept: application/json',
            'Content-Type: application/json',
            'Cookie: device_view=full'
        ),
        ));

        if($password != $password_confirmation){
            $msg = "passwords doesn't match";
        }else {
            $msg = "passwords match";
        }
        $response = curl_exec($curl);
        curl_close($curl);
        $registerArr= json_decode($response);
        if(!EMPTY($registerArr->message)){ // Bad credentials
            return $this->showRegistrationForm();
        }else{
            return redirect()->route('overview');
        }
       
    }

and here is my blade view. I would like to get the countries using the dropdown

 <div >
      <div >
           <select  id="country" name="country" required>
               <option selected="true" disabled="disabled">Country</option>
                   @foreach($countries as $country)
                      <option value="{{$country->country_code}}"> 
                       {{$country->country_name}}</option>
                    @endforeach
                </select>
             </div>
           </div>

and how do I know about the value name and text name? Because the API only showing the registration form without the value. Can someone help me? thank you

CodePudding user response:

Your postfields should be an array :

 CURLOPT_POSTFIELDS => [
            "country" => $country,
        ],

CodePudding user response:

Instead of using

$request->input('country');

you should use

$request->country;

Maybe it will helpful

  • Related