thinkphp发起网络请求

常规做法使用CURL方法:

private function http_request($url,$data = null,$headers=array()){
    $curl = curl_init();
    if( count($headers) >= 1 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

具体使用:

$res = $this->http_request($url,$data);

还有一种:使用 file_get_contents:

public function httpfun(){
    $loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appid."&secret=".$this->secret."&js_code=".$code."&grant_type=authorization_code";
    $res = file_get_contents($loginUrl);
    $wxres = json_decode($res,true);
    echo $wxres;
}

 

posted @ 2018-03-07 14:32  帅到要去报警  阅读(4627)  评论(0编辑  收藏  举报