Problems, need help? Have a tip or advice? Post it here.
2 posts Page 1 of 1
Good Morning @All,

I am trying to read data from a URL. The data is available in JSON format.

I tried the cms:func. With the post by @trendoman.

Code: Select all
<cms:func 'remote_url' url=k_site_link><cms:ignore>
   
        // Sample usage: <cms:call 'remote_url' url="https://www.couchcms.com/" />
   
    </cms:ignore>
    <cms:php>
        if( extension_loaded('curl') ){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "<cms:show url />" );
            curl_setopt($ch, CURLOPT_USERAGENT, 'CouchCMS <cms:show k_cms_version />');
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($ch);
            curl_close($ch);
            echo $output;
        }
    </cms:php>
</cms:func>

<cms:capture into='commits' is_json='1' >
    <cms:call 'remote_url' url='https://api.github.com/repos/CouchCMS/CouchCMS/commits?per_page=5' />
</cms:capture>

<cms:each commits as='entry' startcount='1'>
    <cms:html_encode>
        <cms:date date=entry.commit.committer.date format='j M' />: <cms:show entry.commit.message />
    </cms:html_encode>
    <br/>
</cms:each>


But no value is fetched. How do I get the values from the JSON using URL? Any help would be appreciated.

Regards,
GXCPL

P.S.: URL I'm using is "https://api.covid19india.org/raw_data3.json"
Image
where innovation meets technology
"No value" - perhaps your PHP installation is not configured to have cURL enabled/installed. Obvious solution is to install+enable cURL. Very easy to find out if that is exactly the reason by adding an alternative output for the condition -
Code: Select all
if( extension_loaded('curl') ){
   ...
} else {
   die('cURL not loaded');
}


P.S. The reason my original function 'remote_url' is *not* based on Couch-provided solution is a good one. GitHub, for instance, requires User-Agent sent along the request, whilst Couch does not send user-agent. So, while it perfectly works for the given URL, I still recommend the original cms:func, because it can be tailored to all kinds of necessities with minor PHP tweaks and already works out of the box for most websites.

P.P.S. It is much more comfortable to initiate the request by the browser, client-side, to avoid slow page loading. I only use JS Ajax calls for my clients to make things as smooth as possible. Also, JS works natively with JSON which makes things simpler.
2 posts Page 1 of 1