1
\$\begingroup\$

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]);
    }
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Please edit your title to descrube what your code is doing, not what you want as a review. \$\endgroup\$
    – Marc-Andre
    May 10, 2016 at 12:18

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Ah how you connected two string like this? $url = $url . 'website/api/v1/pro/'.$id;? \$\endgroup\$ May 10, 2016 at 12:37
  • \$\begingroup\$ i don't like operator $a .= "x"; \$\endgroup\$
    – Nick
    May 10, 2016 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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