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]);
}