englishCode Widget displays SJC gold exchange rate

Code widget hiển thị tỷ giá vàng SJC

Today, I am sharing a code to display the price of SJC gold. This code eliminates the need for an extra plugin, making it simple and efficient. The code uses cURL to retrieve the data from the SJC website and displays the gold prices. It includes a function to get the content using cURL and another function to display the gold prices. If the code is inserted into a widget and does not display properly, additional code can be added to the functions.php file of the current theme.

Hey there! Today, I’m excited to share some code to display the SJC gold price without the need for an additional plugin. This simple solution will save you time and hassle.

So, here’s the code snippet to show the SJC gold price. By using this code, you can fetch the latest gold prices seamlessly.

function getContentBycURL($url)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $headers = array(
       "Accept: application/json",
       "Content-Type: application/json",
       "Content-Length: 0",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    //for debug only!
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $resp = curl_exec($curl);
    curl_close($curl);
    return $resp;
}

function tygiavang_sjc()
{   
    $data1 = getContentBycURL('https://www.sjc.com.vn/xml/tygiavang.xml');
    $data = new SimpleXMLElement($data1);
    ob_start();    
?>

    <table border="1">
        <tr bgcolor="yellow">
            <td>Thành phố</td>
            <td>Loại</td>
            <td>Bán ra</td>
            <td>Mua vào</td>
        </tr>
        <?php foreach($data->ratelist->city as $item)
        {
        ?>
            <tr>
                <td <?php if(count($item)>1){?>rowspan="<?php echo count($item)?>" <?php }?>><?php echo (string)$item['name']?></td>
                <?php 
                $first=reset($item->item);
                next($item);
                ?>
                <td><?php echo (string)$first['type']?></td>
                <td><?php echo (string)$first['sell']?></td>
                <td><?php echo (string)$first['buy']?></td>
            </tr>
            <?php 
            if(count($item)>1)
            {
                $isFirst = true;
                foreach($item->item as $t)
                {
                    if ($isFirst) 
                    {
                        $isFirst = false;
                        continue;
                    }   
                ?>
                    <tr>
                        <td><?php echo (string)$t['type']?></td>
                        <td><?php echo (string)$t['sell']?></td>
                        <td><?php echo (string)$t['buy']?></td>
                    </tr>
                <?php 
                }
            } 
        }
        ?>
    </table>
    <?php 
    $tygia_sjc = ob_get_contents();
    ob_end_clean();
    return $tygia_sjc;     
}

add_shortcode('tygiavang_sjc','tygiavang_sjc');

If you encounter any issues with the widget not displaying correctly, simply add the following code snippet to the functions.php file of your current theme.

add_filter( 'widget_text', 'do_shortcode' );

Here’s a preview of how the code widget to show the SJC gold price might look:

See also  Optimize WordPress and WooCommerce for a lighter website experience

Code widget hiển thị tỷ giá vàng SJC

Thanks for stopping by! Feel free to reach out if you have any questions.

Rate this post

Related posts