How to optimize Method Visibility and code structures for better performance? I'm creating an application to get data cross domain all the line of code seem work as well but I'm not much understand about performance with my code process with the real server and real action because sometime there are a lot of case will happen when I fetch those data so I want to make a method to get those errors by using getHttpCode method and however fetch and initial data in this httpData method. Moreover I want to know when I go to real server and real real time It will be facing a lot of case so I want to check that cause or errors and send it to view by using s_respond mean that Server respond with HTTP_code.

Please check my below function

    private function httpData($url =null, $id = null)
    {
        if($id){   
            $url = 'website/api/v1/pro/'.$id;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLINFO_PRETRANSFER_TIME, 30);
        curl_setopt($ch, CURLINFO_HTTP_CODE, true);
        curl_setopt($ch, CURLOPT_PRIVATE, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        if (!$executed = curl_exec($ch)) {

            $res = $executed;
            $data = false;
            curl_close($ch);
        } else {

            if ($this-&gt;http_code = $this-&gt;getHttpCode(curl_getinfo($ch))) {

                $res = $this-&gt;http_code;
                $data = $executed;
            } else {
                $res = false;
            }
        }
        return ['s_respond' =&gt; $res, 'data' =&gt; $executed];
    }

    private function getHttpCode($http)
    {
        if (is_array($http)) {
            if (!empty($http['http_code'] || $http['http_code'] != 0)) {
                return $http['http_code'];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

//Initial and send to view function....


public function products()
    {
        $url = 'http://website/api/v1/products';
        $data = $this-&gt;httpData($url);
        return view('products.list', ['data'=&gt;$data]);
    }
Everything looks good enough.

Code style is OK.

You are using curl, so it will be quite fast and there is nothing you can do in the code to speed it up.

You may or may not need to check this:

    if($id){   // (1)
        $url = 'website/api/v1/pro/'.$id; // (2)
    }


depends from where this $id comes, you probably need to sanitize it somehow. see (1)

In same place you overwrite the $url. see (2) You probably want this:

    if($id){   
        $url = $url . 'website/api/v1/pro/'.$id;
    }


I also do not see where you do curl_close($ch) in case an error happen.