How I can call restful web services efficiently

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
diptaSoumya
Forum Newbie
Posts: 1
Joined: Sat May 04, 2013 1:42 am

How I can call restful web services efficiently

Post by diptaSoumya »

My code is as follow

I need to use this code to fetch data from web service and show them in a site properly but after I request for the webservice I am unable to load them fast. Site is loading very slow.

***** Please use PHP Code tag around source code *****

Code: Select all

<?php

$jRequestURLPrefix = 'http://demo.4ds.com/rema/1.1/';

$menu_json_url  = $jRequestURLPrefix."rules/origins/b2c.json";

$menu_data      = get_json_data($menu_json_url);

/* Function get_json_data definition */

function get_json_data($json_url, $return_array = true, $print_array = false, $curl = true )
{
    $jsonString = '';
    $data       = array();
    if (!$curl)
    {
        /*
         *  if !$curl, use "file_get_contents" method
         *  to get JSON encoded string
        */
        $jsonString = file_get_contents($json_url);

    }
    else
    {
        /*
         *  if $curl, use "curl" method
         *  to get JSON encoded string
        */

        // Initializing curl
        $ch = curl_init( $json_url );

        // Configuring curl options
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
        );

        // Setting curl options
        curl_setopt_array( $ch, $options );

        // Getting JSON encoded string
        $jsonString =  curl_exec($ch); 
    }


    // convert the JSON encoded string into a PHP variable(array)
    //if($return_array)
        $data = json_decode($jsonString, $return_array);

    // $print_array == true, print the array
    if($print_array)
    {
        echo '<pre>';
        print_r($data);
        echo '</pre>';
    }

    return $data;

}

?>
After using the above procedure I am getting the response very slow. Can anyone please help If I need to change any steps here
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How I can call restful web services efficiently

Post by mecha_godzilla »

Hi,

I've tested this on my machine and the script takes about 3 seconds to run in total - is this similar to the kind of speed you're getting? I'm also on a *very* slow connection, so I would expect the true figure to be in the region of just over 1 second.

If this data is unlikely to change very often you could always cache it locally - some servers will send a "Last-Modified" value in the headers, which (used in conjunction with a cron job) would allow you to refresh the cache as a background process. If you can't retrieve a "Last-Modified" value from the headers then you could also run the code through a one-way hash to see whether any of the information in it has changed.

HTH,

Mecha Godzilla
Post Reply