I am using Drupal-6. I Created CCK Field with Select Box with content type name as list_porducts.

i am fetching products from external url . Here is my code

$key=array();
$flowervalue=array();
for($i=2;$i<=150; $i++) {
        $url="http://testurl.com/test/node/".$i.".jsonp";
        $timeout=0;
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        $jdata=json_decode($data);  


            if($jdata->type=='product') {
                $key[]=$jdata->nid;
                $flowervalue[]=$jdata->title." ".$jdata->field_petals_code[0]->value;



            }   



}
$res=array_combine($key, $flowervalue);
return $res;

which is running properly. Now Problem is this is Quite slow. and i guess it is running for all page load.

Any Idea how to do this.

Thanks

share|improve this question

1 Answer

up vote 1 down vote accepted

Store the result into a session like below

function get_all_products() {
  $prod = get_products();
}

function get_products($reset = FALSE) {
  $res = $_SESSION['products'];
  if(empty($res) || $reset) {
    $key=array();
    $flowervalue=array();
    for($i=2;$i<=150; $i++) {
            $url="http://testurl.com/test/node/".$i.".jsonp";
            $timeout=0;
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $data = curl_exec($ch);
            $jdata=json_decode($data);  

                if($jdata->type=='product') {
                    $key[]=$jdata->nid;
                    $flowervalue[]=$jdata->title." ".$jdata->field_petals_code[0]->value;



                }   



    }
    $res=array_combine($key, $flowervalue);
    $_SESSION['products'] = $res;
  }
  return $res;
}

Else if you want cache the $url="http://testurl.com/test/node/".$i.".jsonp" enable the page caching in drupal.

share|improve this answer
:Thanks a ton. it hepls me lot :) – Hitu Bansal Feb 24 '12 at 4:53

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.