Return Curl with JSON

Asked

Viewed 1,189 times

1

Good morning, I’m trying to return this code in JSON, I just want some specific fields ex: Operator, type, portability and status.

<?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);
?>

That’s my code and 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 case I would just like that return on 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>
  • Could inform the search result (format .. etc ...)

  • No.... I want you to put in your question the result you are getting from your query. Because we can not guess how the return of the request is coming, It helps everyone to better understand your problem.

  • Okay, I made the changes.

1 answer

0


The page http://consultanumero.info/consulta does not return a json object from php, instead returns in the format text/html. Then you should scroll through the returned html document, search for the elements (tags you want) and take their values (content). The following example (based on your code) does these operations. I put the comments in the code itself to facilitate understanding. But generally the string returned when accessing the url is converted into a class object Domdocument, and from there 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"}
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.