Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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->http_code = $this->getHttpCode(curl_getinfo($ch))) {

                $res = $this->http_code;
                $data = $executed;
            } else {
                $res = false;
            }
        }
        return ['s_respond' => $res, 'data' => $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->httpData($url);
        return view('products.list', ['data'=>$data]);
    }
share|improve this question
2  
Please edit your title to descrube what your code is doing, not what you want as a review. – Marc-Andre May 10 at 12:18
up vote 1 down vote accepted

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.

share|improve this answer
    
Ah how you connected two string like this? $url = $url . 'website/api/v1/pro/'.$id;? – Heng Sopheak May 10 at 12:37
    
OH I understand thanks – Heng Sopheak May 10 at 12:43
    
i don't like operator $a .= "x"; – Nick May 10 at 13:21

Your Answer

 
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.