简体   繁体   English

使用 Z2FEC392304A5C23AC138DA22847F97B 从 URL 获取 JSON object

[英]Get JSON object from URL using PHP

I have a URL that gives different responses in JSON.我有一个 URL 在 JSON 中给出不同的响应。 I need to get the response using PHP and display target_url.我需要使用 PHP 获得响应并显示 target_url。 I did it successfully with this code.我用这段代码成功地做到了。

<?php
        $content = file_get_contents("https://code.directadvert.ru/show.cgi?adp=5357330&json=22");
        $content = utf8_encode($content);
        $result = json_decode($content, true);
        $url = $new['target_url'];

        foreach($result['news'] as $new) {
            echo $new['target_url'];
        }
?>

But I get only one of two identical answers.但我只得到两个相同答案之一。 And every time I have to receive it differently.每次我必须以不同的方式接收它。 What could be the problem?可能是什么问题呢?

You already have the JSON.. No need for a loop.. Just drill down to the URL..你已经有了 JSON.. 不需要循环.. 只需深入到 URL..

<?php
    $content = file_get_contents("https://code.directadvert.ru/show.cgi?adp=5357330&json=22");
    $content = utf8_encode($content);
    $result = json_decode($content, true);

    $url = $result['news'][0]['target_url'];
    
    echo $url;

When you json_decode -- Your $result becomes:当你json_decode -- 你的$result变成:

Array
(
    [news] => Array
        (
            [0] => Array
                (
                    [id] => 10906072
                    [pubdate] => 1602148428
                    [site_name] => fashion2news.com
                    [img] => https://cdn.directadvert.ru/cdn/images/100x100/40/8154740.jpg
                    [target_url] => https://fashion2news.com/tops-tees/diana-gurckaja-pritvorjalas-slepoj-40-let/?utm_medium=MarketGid&utm_source=MarketGid&utm_campaign=MarketGid&utm_term={widget_id}&utm_content=3219275&MarketGid
                    [rating] => 3
                    [title] => Ëîæü è öèíèçì ãîäàìè: ñòðàíà ñìîëêëà, óçíàâ, êåì îêàçàëàñü Ãóðöêàÿ
                    [created] => 1602148428
                    [url] => https://code.directadvert.ru/click/?x=Z8162cWaXn9LS96s2B_LJkN-p8fHQJjfLQ_0_mz4prYJYHjAAs3ju0g8WZ7WYJ_XhxC6dJJXzv-ZQOEYs6qWU1S2E0tjHoxxwhTmEUQ8XxOjvXW_zkKVkGbwg8OyXFW3vtcNSjmyV4BUu7cG9lMNBNiQh7x4WZBMGf9ODf60XOQZJhHU_lLugd4xKLFyqn8qGam46cVP-jnkQfINq3KkvSWh0rr9Q0TJtsKda-Cny04r-XGSb0cSKtKsHnjgkUq51iLgqMavsf-Z2Dp7exnyDA40tNIEKvf-ovZQCZvc3vz1A4f0fTfEtwlQyAV9MlIaDu7-2Lijh0Y1Tmox76ipgGXYHPyRVGpKD2E8Gh5wymj9jVVnMmIEYIqnIeTW0FHf6CLaV1hbziS8reZmfidytR79XxVWIO3puMtYk72qpnR0HcIj2dR8DhkbdC_K6Li0p_k7A5wRmaSF4L4SkyxkihSvV7V-gJif-CgYEzldqB1CkePDs1RWnD3ZTANj8m2FGBBxPPNsWqgROhAAkDLatuyBfYnCB4DSNotVyBNe6oY
                )

        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM