2

Eu já pesquisei em todos os sites, porém não consegui achar uma forma de pegar o valor da totalPage.

Estou lendo Json com o PHP e preciso pegar esse valor pra fazer a paginação.

Segue abaixo os dados.

object(stdClass) # 1(3) {
    ["requestInfo"] =>
    object(stdClass) # 2(3) {

        ["status"] =>
        string(2)"OK"
        ["message"] =>
        string(7)"SUCCESS"
        ["generatedDate"] =>
        NULL

    }
    ["pagination"] =>
    object(stdClass) # 3(4) {
        ["page"] =>
        int(1)
        ["size"] =>
        int(10)
        ["totalSize"] =>
        int(1400)
        ["totalPage"] =>
        int(140)
    }
    ["offers"] =>
    array(10) {
        [0] =>
        object(stdClass) # 4(10) {
            ["id"] =>
            string(8)"11248674"
            ["name"] =>
            string(156)"Smartphone Motorola Moto G5 Plus TV XT1683 Platinum com 32GB, Tela 5.2 ´ ´, Dual Chip, Android 7.0, 4G, Câmera 12MP, Processador Octa - Core e 2GB de RAM"
            ["product"] =>
            object(stdClass) # 5(11) {
                ["id"] =>
                int(630970)
                ["name"] =>
                string(40)"Smartphone Motorola Moto G 5 Plus XT1683"
                ["shortName"] =>
                string(22)"Motorola 5 Plus XT1683"
                ["priceMin"] =>
                float(891.65)
                ["priceMax"] =>
                float(1819)
                ["discount"] =>
                int(50)
                ["thumbnail"] =>
                object(stdClass) # 6(4) {
                    ["url"] =>
                    string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_600x600-PU9a0ba_1.jpg"
                    ["height"] =>
                    int(600)
                    ["width"] =>
                    int(600)
                    ["otherFormats"] =>
                    array(4) {
                        [0] =>
                        object(stdClass) # 7(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_100x100-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(100)
                            ["width"] =>
                            int(100)
                        }
                        [1] =>
                        object(stdClass) # 8(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_200x200-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(200)
                            ["width"] =>
                            int(200)
                        }
                        [2] =>
                        object(stdClass) # 9(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_300x300-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(300)
                            ["width"] =>
                            int(300)
                        }
                        [3] =>
                        object(stdClass) # 10(3) {

                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_600x600-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(600)
                            ["width"] =>
                            int(600)
                        }
                    }
                }
                ["userRating"] =>
                object(stdClass) # 11(2) {
                    ["comments"] =>
                    int(226)
                    ["rating"] =>
                    int(9)
                }
                ["category"] =>
                object(stdClass) # 12(5) {
                    ["id"] =>
                    int(77)
                    ["name"] =>
                    string(20)"Celular e Smartphone"
                    ["thumbnail"] =>
                    object(stdClass) # 13(1) {
                        ["url"] =>
                        string(50)"http://imagem.buscape.com.br/bp5/categorias/77.jpg"
                    }
                    ["hasProduct"] =>
                    int(444)
                    ["hasOffer"] =>
                    int(8297)
                }
                ["hasOffer"] =>
                int(70)
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            }
            ["category"] =>
            object(stdClass) # 14(6) {
                ["id"] =>
                int(77)
                ["name"] =>
                string(20)"Celular e Smartphone"
                ["thumbnail"] =>
                object(stdClass) # 15(1) {

                    ["url"] =>
                    string(50)"http://imagem.buscape.com.br/bp5/categorias/77.jpg"
                }
                ["hasProduct"] =>
                int(444)
                ["hasOffer"] =>
                int(8297)
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            }
            ["link"] =>
            string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            ["thumbnail"] =>
            string(61)"https://thumbs.buscape.com.br/T100x100/__2.903294-dbe7fcb.jpg"
            ["price"] =>
            float(891.65)
            ["discount"] =>
            int(50)
            ["installment"] =>
            object(stdClass) # 16(2) {
                ["quantity"] =>
                int(10)
                ["value"] =>
                float(104.9)
            }
            ["store"] =>
            object(stdClass) # 17(6) {
                ["id"] =>
                int(5756)
                ["name"] =>
                string(11)"Casas Bahia"
                ["thumbnail"] =>
                string(46)"https://www.lomadee.com/vitrine/logo903294.gif"
                ["ebit"] =>
                object(stdClass) # 18(3) {

                    ["id"] =>
                    int(30)
                    ["name"] =>
                    string(12)"e-bit Ótima"
                    ["rating"] =>
                    int(4)
                }
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
                ["ocb"] =>
                bool(false)
            }
        }
2
  • Insira o json completo a pergunta
    – WMomesso
    29/10/2017 às 21:19
  • Bom dia, adicionei o código completo, dá uma olhada 31/10/2017 às 10:24

2 Respostas 2

3

Retorne os valores como json e use json_decode(), que retornará um objeto ou matriz se o segundo valor for verdadeiro. Exemplo:

$json = '{"paisId":"84","produtoId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['paisId'];
echo $json['produtoId'];
echo $json['status'];
echo $json['opId'];
2
  • Fiz da forma que você passou, mais não voltou nenhum resultado, fiz dessa forma $json = json_decode($json_file, true); echo $json['totalPage']; 2/11/2017 às 19:31
  • Faça um var_dump($json); e me diga o que retornou.
    – Pedro Igor
    3/11/2017 às 20:30
0

Note que Você tem retorno de Objetos não arrays (o true do json_decode o converte em um array associativo, particularmente prefiro trabalhar com objetos). Fique atento na hierarquia dos elementos, senão ficará impossível manipulá-los.

Então para manipular os valores (como objetos) deverá usar:

$objeto = json_decode($json);
echo $objeto->pagination->totalPage;

No caso do array associativo:

$associativo = json_decode($json, true);
echo $associativo["pagination"]["totalPage"];

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .