Login light

curl

function curl_get($url){
  echo $url;
  //初始化
  $ch = curl_init();
  //设置选项,包括URL
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  //执行并获取HTML文档内容
  $output = curl_exec($ch);
  //释放curl句柄
  curl_close($ch);
  //打印获得的数据
  return $output;
}

public static function curl_call($url, $data = null, $is_post = false, $options = null) 

    $curl = curl_init();

    if (is_array($data)) {
        $data = http_build_query($data);
    }

    if ($is_post) { //POST数据
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    else { //GET
        if (!empty($data)) { //如果还给出了额外的data,就拼入url
            $sep = '?';
            if (strpos($url, '?') !== false) { //已经有参数了
                $sep = '&';
            }
            $url .= $sep . $data;
        }
    }

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);

    //不验证SSL证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    if (is_array($options)) { //额外的curl option
        foreach($options as $key => $value) {
            curl_setopt($curl, $key, $value);
        }
    }

    $ret_data = curl_exec($curl);

    if (curl_errno($curl)) {
        curl_close($curl);
        return false;
    }
    else {
        curl_close($curl);
        return $ret_data;
    }
}