1

I am facing one issue. I am fetching the product details as per the category using magento2-REST API but its taking long time ti give the response as less nos of data is there. I am explaining my code below.

$banner=array(array("image"=>"http://domain.com/pub/media/wysiwyg/smartwave/porto/01_1.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/smartwave/porto/02_1.jpg"));
$sectionImage=array(array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/12_1.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/20.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/7.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/17.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/16.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/14.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/13.jpg"),array("image"=>"http://domain.com/pub/media/wysiwyg/alothemes/12.jpg"));
$homePageArr=array("topBanner"=>$banner,"sectionBanner"=>$sectionImage,"MainCategory"=>array());
$catArr=array(array("cat_id"=>2),array("cat_id"=>147),array("cat_id"=>92));
$newArrivalArr=array();
$dealOfDay=array();
$dealOfHour=array();
$trending=array();
$summer=array();
$hotDealArr=array();
foreach ($catArr as $key => $value) {
    $cat_id=$value['cat_id'];
    $ch = curl_init("http://domain.com/index.php/rest/V1/categories/".$cat_id."/products/");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
    $res = curl_exec($ch);
    $resu = json_decode($res, 1);
    foreach ($resu as $val) {
        $sku=$val['sku'];
        $catid=$val['category_id'];
        $result='';
        $result1='';
        if ($catid==2) {
            $ch1 = curl_init("http://domain.com/index.php/rest/V1/products/".$sku);
            curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch1, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
            $result = curl_exec($ch1);
            $result1 = json_decode($result, 1);
            if (isset($result1['custom_attributes']['news_from_date']) && isset($result1['custom_attributes']['news_to_date'])) {
                $today=strtotime(date("Y-m-d h:i:s"));
                $from_date=strtotime($result1['custom_attributes']['news_from_date']);
                $to_date=strtotime($result1['custom_attributes']['news_to_date']);
                if ($today > $from_date && $today < $to_date) {
                    $newArrivalArr[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
                }
                $ret=compareDate($result1['custom_attributes']['news_from_date']);
                if ($ret==1 || $ret=='1') {
                    $dealOfDay[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
                }
                if ($ret==2 || $ret=='2') {
                    $dealOfHour[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
                }
            }
            if (isset($result1['custom_attributes']['special_from_date']) && isset($result1['custom_attributes']['special_to_date'])) {
                $today=strtotime(date("Y-m-d h:i:s"));
                $from_date=strtotime($result1['custom_attributes']['special_from_date']);
                $to_date=strtotime($result1['custom_attributes']['special_to_date']);
                if ($today > $from_date && $today < $to_date) {
                    if (isset($result1['custom_attributes']['special_price'])) {
                        if (int($result1['custom_attributes']['special_price']) < $result1['price']) {
                            $hotDealArr[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
                        }
                    }
                }
            }
        }else{
            $ch1 = curl_init("http://domain.com/index.php/rest/V1/products/".$sku);
            curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch1, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
            $result = curl_exec($ch1);
            $result1 = json_decode($result, 1);
            //print_r($result1);exit;
            if ($catid==147) {
                $trending[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
            }
            if ($catid==92) {
                $summer[]=array("image"=>$imagepath.$result1['media_gallery_entries'][0]['file'],"name"=>$result1['name'],"id"=>$result1['id'],"price"=>$result1['price']);
            }
        }
    }
}
if (count($newArrivalArr) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'New Arrival',"data"=>$newArrivalArr);
}
if (count($dealOfDay) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'Deals of the day',"data"=>$dealOfDay);
}
if (count($dealOfHour) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'Deals of the hour',"data"=>$dealOfHour);
}
if (count($trending) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'Trending',"data"=>$trending);
}
if (count($summer) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'Summer Collection',"data"=>$summer);
}
if (count($hotDealArr) > 0) {
    $homePageArr['MainCategory'][]=array("name"=>'Hot Deal',"data"=>$hotDealArr);
}
echo json_encode($homePageArr); 

Here I have 3 category id and want to fetch the all data. As there are less number of data are coming as response but its taking too much time to give the response. Here I need to reduce the response time.

Your Answer

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

Browse other questions tagged or ask your own question.