Return curl with JSON

1

Hello, I'm trying to return this code in JSON, I just want some specific fields eg: Operator, type, portability and state.

<?php

//header('Content-type: application/json');
$post = ['tel' => $_GET['enviar'] , 'bto' => 'submit'];

$ch = curl_init('http://consultanumero.info/consulta');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'http://consultanumero.info/consulta',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
    ],
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_ENCODING => '',
    CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS
]); 
//json_encode($ch);
echo $output = curl_exec($ch);
?>

This is my code and the return:

<!DOCTYPE html>

<html lang="pt-br">

<head>
    <meta charset="utf-8" />
    <title>Consulta Número</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta property="fb:app_id" content="519584384854184" />
    <link rel="shortcut icon" href="/favicon.ico" />
    <link rel="stylesheet" href="/css/base.css?1409782940" media="all" />
    <link rel="prerender" href="http://consultanumero.info/" />
    <script src="/js/base.js?1407968504" async></script>
    <script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" async></script>
    <script>
        (function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
        ga("create", "UA-16052289-33", "auto");
        ga("send", "pageview");
    </script>
</head>

<body class="int consulta">

<div id="geral">
<div id="cab">
    <h1><a href="/">Consulta Número</a></h1>
    <p>Descubra a operadora atual de qualquer número, celular ou telefone fixo!</p>
</div>



<div id="ctd">
    <div class="prop">
        <ins class="adsbygoogle" style="display: inline-block" data-analytics-uacct="UA-16052289-33" data-ad-client="ca-pub-3806187316222201" data-ad-slot="9873102520"></ins>
        <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
    </div>

    <div class="resultado">
        <div class="a">
            <img src="/img/op/claro.png" alt="Claro" title="Claro" />
            <div class="tel">(11) 97612 0592</div>
        </div>

        <div class="b">
            <p><span>Tipo <span>&raquo;</span></span> Celular</p>
            <p><span>Portabilidade <span>&raquo;</span></span> Não</p>
            <p><span>Estado <span>&raquo;</span></span> São Paulo (SP)</p>
            <p><span>Região <span>&raquo;</span></span> São Paulo e região metropolitana</p>
        </div>
    </div>

    <button class="li" onclick="ga('send', 'event', 'Consulta', 'Ligar'); window.location = 'tel:011976120592'">Ligar</button>
    <button class="nc" onclick="ga('send', 'event', 'Consulta', 'Nova'); window.location = '/'">Nova Consulta</button>
</div>

In the case I would only like this return in JSON.

<div class="resultado">
        <div class="a">
            <img src="/img/op/claro.png" alt="Claro" title="Claro" />
            <div class="tel">(11) 97612 0592</div>
        </div>

        <div class="b">
            <p><span>Tipo <span>&raquo;</span></span> Celular</p>
            <p><span>Portabilidade <span>&raquo;</span></span> Não</p>
            <p><span>Estado <span>&raquo;</span></span> São Paulo (SP)</p>
            <p><span>Região <span>&raquo;</span></span> São Paulo e região metropolitana</p>
        </div>
    </div>
    
asked by anonymous 10.07.2017 / 16:16

1 answer

0

The http://consultanumero.info/consulta page does not return a json object from php, instead it returns in text/html format. Then you should go through the html document returned, look for the elements (tags you want) and get their values (content). The following example (based on your code) does these operations. I put the comments in the code itself to make it easier to understand. But in general the string returned when accessing the url is converted into an object of class DOMDocument , and the from it are accessed the nos of the html document.

<?php
header('Content-Type: application/json');

//numero de teste
$post = ['tel' => '21996787878' , 'bto' => 'submit'];

$ch = curl_init('http://consultanumero.info/consulta');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'http://consultanumero.info/consulta',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
    ],
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_ENCODING => '',
    CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS
]); 

//pagina html com a resposta
$output = curl_exec($ch);

/************************************************************
A partir da variavel $output, deve-se procurar as informações
relevantes do html
************************************************************/

//loadHTML retorna um objeto representando o DOM do arquivo html
$documento = DOMDocument::loadHTML($output);
//retorna um Objeto DOMListNode, que é composto por obejetos 
// DOMElement (que representam cada elemento do html com a tag <p>)
$tags = $documento->getElementsByTagName('p');

//array para montar o objeto json a ser retornado pelo script atual
$campos_json = [];

//a variavel $tag representa um objeto DOMElement (uma tag do documento html) 
//de onde pode-se obter os filhos dessa tag
foreach($tags as $tag){
    //como a variavel $tags tem paragrafos que não possui a mesma hierarquia que os paragrafos com as informações buscadas,
    //checar se esses atributos existem
    if($tag->firstChild !== null && 
        $tag->lastChild !== null){
        if($tag->firstChild->firstChild !== null){
            $chave = trim($tag->firstChild->firstChild->nodeValue);
            $valor = trim($tag->lastChild->nodeValue);
            $campos_json[$chave] = $valor;
        }
    }
}

//imprime resultado final em json
echo json_encode($campos_json);

The above code generates as output:

{"Tipo":"Celular","Portabilidade":"N\u00e3o","Estado":"Rio de Janeiro (RJ)","Regi\u00e3o":"Rio de Janeiro e regi\u00e3o metropolitana"}
    
10.07.2017 / 23:21