Difficulties in receiving JSON data in PHP

0

Good evening everyone, I'm having a serious question about receiving JSON data via an external link.

I have a link that pulls all the information I need to show, but there is my question, I'm using a form to show the data according to the change , in which case it is divided by: City , Category , Specialty and Service . Being that the result will only be shown after I fill these options. However every city that will be chosen will be shown first to the advertisers being that the way I am using is appearing all at once that leaves the site very slow.

The code that pulls the information I'm using is:

<?php
    $conveniados = array();
    $destaques = array();
    $servicos = array();
    $locais = array();
    $especialidades = array();

    $convs = json_decode(file_get_contents('http://gcfreitas.com.br/AfaguService/BackOffice.svc/retornaConveniado'));
    foreach($convs as $conv){
        $info = array();
        $info["nome"] = $conv->nmConveniado;
        $info["categoria"] = $conv->listaEspecialidade[0]->nmEspecialidade;
        $especialidades[] = $conv->listaEspecialidade[0]->nmEspecialidade;
        $info["endereco"] = $conv->listaEndereco[0]->nmLogradouro;
        $info["bairro"] = $conv->listaEndereco[0]->nmBairro;
        $info["cidade"] = $conv->listaEndereco[0]->nmCidade;
        $info["telefone"] = $conv->dsTelefone1;
        $locais[] = $conv->listaEndereco[0]->nmCidade;
        $info["estado"] = $conv->listaEndereco[0]->sgEstado;
        $servicos_str = '';
        $k=0;
        foreach($conv->listaServico as $serv_str){
            $k++;
            $servicos[] = $serv_str->nmServico;
            if($k==count($conv->listaServico)){
                $servicos_str .= $serv_str->nmServico;
            } else {
                $servicos_str .= $serv_str->nmServico.', ';
            }
        }
        $info["servicos"] = $servicos_str;
        $info["imagem"] = $conv->listaImagem[0]->nmArquivo;
        $conveniados[] = $info;
        if($conv->flDestaqueSite){
            $destaques[] = $info;
        }
    }
    $_especialidades = array_unique($especialidades);
    $_servicos = array_unique($servicos);
    $_locais = array_unique($locais);
    asort($_especialidades);
    asort($_servicos);
    asort($_locais);
    ?>

Selection is:

<div class="main-search-input">

                        <div class="main-search-input-item">
                            <input type="text" placeholder="Nome do conveniado" id="search" value="">
                        </div>

                        <div class="main-search-input-item">
                            <select data-placeholder="Cidade" class="chosen-select" onchange="swapContent(this);" id="search" style="display: none;">
                            <option value="">Cidade</option>
                                <?php foreach($_locais as $local){ ?>
                    <?php if(trim($local)!=''){ ?>
                    <option value="<?php echo $local; ?>" id="search"><?php echo $local; ?></option>
                    <?php } ?>
                <?php } ?>
                            </select>
                        </div>

                        <!--div class="main-search-input-item">
                            <select data-placeholder="Categoria" class="chosen-select" id="search" style="display: none;">
                                <option>Categoria</option>  
                            </select>
                        </div-->

                        <div class="main-search-input-item">
                            <select data-placeholder="Especialidade" class="chosen-select" id="search" style="display: none;">
                                <?php foreach($_especialidades as $especialidade){ ?>
                    <?php if(trim($especialidade)!=''){ ?>
                    <option value="<?php echo $especialidade; ?>" id="<?php echo $especialidade; ?>"><?php echo $especialidade; ?></option>
                    <?php } ?>
                <?php } ?>
                            </select>
                        </div>
                        <div class="main-search-input-item">
                            <select data-placeholder="Serviço" class="chosen-select" id="search" style="display: none;">
                                <option value>Serviços</option>
                <?php foreach($_servicos as $servico){ ?>
                    <?php if(trim(servico)!=''){ ?>
                    <option value="<?php echo $servico; ?>" id="<?php echo $servico; ?>"><?php echo $servico; ?></option>
                    <?php } ?>
                <?php } ?>
                            </select>
                        </div>
                        <input type="submit" class="button" value="Buscar" />     

                    </div>

And the result is:

<div class="row" id="parent">

                <!-- Lista -->
                <?php $d=0; ?>
            <?php shuffle($destaques); ?>
            <?php foreach($destaques as $destaque){ ?>
                <?php 
                $d++; 
                if($d<=2){ 
                ?>
<div class="col-lg-12 col-md-12" local="<?php echo $destaques["cidade"]; ?>">
                    <div class="listing-item-container list-layout">
                        <a href="#" class="listing-item">

                            <!-- Imagem -->
                            <div class="listing-item-image">
                            <?php if($destaque["imagem"] != ''){ ?>
                                <img data-cfsrc="<?php echo $destaque["imagem"]; ?>" alt="<?php echo $destaque["nome"]; ?>" style="display:none;visibility:hidden;"><noscript><img src="<?php echo $destaque["imagem"]; ?>" alt="<?php echo $destaque["nome"]; ?>"></noscript>
                                <?php } else { ?>
                                    <img data-cfsrc="images/sem-imagem.jpg" alt="<?php echo $destaque["nome"]; ?>" style="display:none;visibility:hidden;"><noscript><img src="images/sem-imagem.jpg" alt="<?php echo $destaque["nome"]; ?>"></noscript>
                                    <?php } ?>
                                <span class="tag"><?php echo $destaque["categoria"]; ?></span>
                            </div>

                            <!-- Conteúdo -->
                            <div class="listing-item-content">
                                <div class="listing-badge anunciante">Anunciante</div>

                                <div class="listing-item-inner">
                                    <h3><?php echo $destaque["nome"]; ?> <i class="verified-icon"></i></h3>
                                    <span><?php echo $destaque["telefone"]; ?></span></br>
                                    <span><?php echo $destaque["endereco"]; ?>, <?php echo $destaque["bairro"]; ?>, <?php echo $destaque["cidade"]; ?> - <?php echo $destaque["estado"]; ?></span>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <?php } ?>
            <?php } ?>

                <!-- Lista / Fim -->

                <center><a href="#" class="button border with-icon margin-bottom-20">Ver mais</a></center>

                <!-- Lista -->
                <?php $d=0; ?>

                <?php foreach($conveniados as $conveniado){ ?>
                <?php 
                $d++; 
                if($d<=20){ 
                ?>
                <div class="col-lg-12 col-md-12 box food" especialidade="<?php echo $conveniado["categoria"]; ?>" local="<?php echo $conveniado["cidade"]; ?>" nome="<?php echo $conveniado["nome"]; ?>" servicos="<?php echo $conveniado["servicos"]; ?>">
                    <div class="listing-item-container list-layout">
                        <a href="#" class="listing-item">

                            <!-- Imagem -->
                            <div class="listing-item-image">
                            <?php if($conveniado["imagem"] != ''){ ?>
                                <img data-cfsrc="<?php echo $conveniado["imagem"]; ?>" alt="" style="display:none;visibility:hidden;"><noscript><img src="<?php echo $conveniado["imagem"]; ?>" alt=""></noscript>
                            <?php } else { ?>
                                <img data-cfsrc="images/sem-imagem.jpg" alt="" style="display:none;visibility:hidden;"><noscript><img src="images/sem-imagem.jpg" alt=""></noscript>
                            <?php } ?>

                            <?php if($conveniado["categoria"] != ''){ ?>
                                <span class="tag"><?php echo $conveniado["categoria"]; ?></span>
                            <?php } else { ?>
                                    <span class="tag">Sem Categoria</span>
                            <?php } ?>
                            </div>

                            <!-- Conteúdo -->
                            <div class="listing-item-content">

                                <div class="listing-item-inner">
                                    <h3 class="name"><?php echo $conveniado["nome"]; ?></h3>

                                    <?php if($conveniado["telefone"] != ''){ ?>
                                    <span><?php echo $conveniado["telefone"]; ?></span></br>
                                    <?php } else { ?>
                                    <span>Telefone não informado</span></br>
                                    <?php } ?>


                                    <span class="local"><?php echo $conveniado["endereco"]; ?>, <?php echo $conveniado["bairro"]; ?>, <?php echo $conveniado["cidade"]; ?> - <?php echo $conveniado["estado"]; ?></span>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <?php } ?>
                <?php } ?>

            </div>

Kindly if anyone can guide me I will be very grateful.

    
asked by anonymous 12.07.2018 / 01:42

1 answer

1

Regarding the delay due to the code below:

$convs = json_decode(file_get_contents('http://gcfreitas.com.br/AfaguService/BackOffice.svc/retornaConveniado'));

It takes almost 10 seconds to run, you could use local cache to increase speed.

About filtering functionality you should place your selects tags within the example form tag. In the foreach statements you must include condition to filter result based on the select example fields:

if($conveniado["cidade"] == $_POST["cidade"]){
// block exibição do anúncio ..
}
    
12.07.2018 / 17:10