laravel:模型中同一类的调用方法

时间:2019-08-03 07:06:33

标签: laravel

在模型中我有函数serverApi,在同一模型中我想调用那些函数,但是命令错误未定义函数 当我调用函数getProvince()时出现错误

里面有serverApi函数

我使用$ this this ::,将公共功能serverApi设为私有,但仍然是相同的问题

class Tracking extends Model
{   
    private function serverApi($url){
        $curl = curl_init();

            curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "key: 1a8aef03546713fdc55870b8b640a2c6"
            ),
            ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {return $err;} else { return $response;}
    }
    public function getProvince(){
        $url = "https://api.rajaongkir.com/starter/province";
        serverApi($url);
    }
}

1 个答案:

答案 0 :(得分:0)

this应该有效:

public function getProvince(){
    $url = "https://api.rajaongkir.com/starter/province";
    return $this->serverApi($url);
}