L
L
Lulzsec2017-04-03 00:39:40
PHP
Lulzsec, 2017-04-03 00:39:40

How to parse such JSON from Telegra.ph?

Telegra.ph has an API that returns the text in a very interesting form: https://api.telegra.ph/getPage/Vozmozhnosti-Telegr...
Can you help me how to bring into a divine (pure HTML) form what is in result ->content?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lexey Felde, 2017-04-03
@Lulzsec

The code is unnecessarily, but it is suitable for an example.

function toHtml(array $content) {
    $html = '';

    foreach ($content as $row) {
       if (is_string($row)) {
            $html .= $row;

            continue;
        }

        $attrs = '';
        if (isset($row['attrs'])) {
            foreach ($row['attrs'] as $name => $value) {
                if ($row['tag'] === 'img') {
                    $value = '//telegra.ph' . $value;
                }
                $attrs .= sprintf(' %s="%s"', $name, $value);
            }
        }

        $children = '';
        if (isset($row['children'])) {
            $children .= toHtml($row['children']);
        }

        $html .= sprintf('<%s%s>%s</%s>', $row['tag'], $attrs, $children, $row['tag']);
    }

    return $html;
}

$content = json_decode(
    file_get_contents('https://api.telegra.ph/getPage/Vozmozhnosti-Telegram-03-30?return_content=true'), 
    true
);

echo toHtml($content['result']['content']);

G
GrimJack, 2017-04-03
@GrimJack

json_decode and beyond to your heart's content

A
Alexander Moiseykin, 2017-04-03
@moiseykin

You can use the functions from their documentation .

D
DevFish, 2017-12-29
@DevFish

Perhaps there are jambs somewhere. I haven't tested much.

protected function generatePostText($content)
    {
        $doc = new \DOMDocument('1.0');
        foreach ($content['content'] as $node) {
            $domElement = $this->nodeToDom($node, $doc);
            if ($domElement !== null) {
                $doc->appendChild($domElement);
            }
        }

        return html_entity_decode($doc->saveHTML());
    }



protected function nodeToDom($node, $doc)
    {
        $elementNode = null;
        if (is_string($node)) {
            return $doc->createTextNode($node);
        }
        if (isset($node['tag'])) {
            $elementNode = $doc->createElement($node['tag']);
            if (isset($node['attrs'])) {
                foreach ($node['attrs'] as $attrName => $attrValue) {
                    if ($attrName === 'src' && $node['tag'] === 'img') {
                        if ($attrValue[0] === '/') {
                            $attrValue = "http://telegra.ph{$attrValue}";
                        }
                    }
                    $elementNode->setAttribute($attrName, $attrValue);
                }
            }
        }
        if (isset($node['children'])) {
            foreach ($node['children'] as $child) {
                $childNode = $this->nodeToDom($child, $doc);
                if ($childNode !== null) {
                    $elementNode->appendChild($childNode);
                }
            }
        }
        return $elementNode;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question