0

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

1

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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