最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

yii2 - Connect an API in controller and display its data in view? - Stack Overflow

programmeradmin1浏览0评论

This is my view file (_form.php) and it was rendered to create.php file. The dropdown work as well if the connection was in view file. The problem is when I try to remove the connection into controller then it did not work. It will display 'No countries found'.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => '.1/all?fields=name',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
));

$response = curl_exec($curl);

curl_close($curl);
$countries = json_decode($response, true);
?>

<div class="card styled-card">
    <div class="card-body">
        <div class="people-form">

            <?php $form = ActiveForm::begin(); ?>

            <---- another inputs code ---->

            <?php
            if (!empty($countries)):
                $countryList = [];
                foreach ($countries as $country) {
                    if (isset($country['name']['common'])) {
                        $countryList[] = $country['name']['common']; // Only store country names
                    }
                }
                
                asort($countryList); // Sort the country names alphabetically

                ?>
                <?= $form->field($model, 'nation')->dropDownList(
                    array_combine($countryList, $countryList), // Using name as both key & value
                    ['prompt' => 'Select a Country']
                )->label('Nation') ?>
            <?php else: ?>
                <p>No countries found.</p>
            <?php endif; ?>


            <---- submit button code ---->

            <?php ActiveForm::end(); ?>

        </div>
    </div>
</div>

This is my controller when I try to remove the code

public function actionCreate()
{
    $model = new People();

    // Fetch countries from the API
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => '.1/all?fields=name',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    $countries = json_decode($response, true);

    if ($this->request->isPost) {
        if ($model->load($this->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
    } else {
        $model->loadDefaultValues();
    }

    // Pass the countries data to the view
    return $this->render('create', [
        'model' => $model,
        'countries' => $countries,
    ]);
}

The dropdown work as well when the connection was in view file

I want to get like this when the connection was in controller.

发布评论

评论列表(0)

  1. 暂无评论