/**
 * GET请求
 * @param $url
 * @return bool|mixed
 */
public function http_get($url)
{
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus ["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * POST 请求
 *
 * @param string $url
 * @param array $param
 * @return string content
 */
public function http_post($url, $param, $header)
{
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
    }
    if (is_string($param)) {
        $strPOST = $param;
    } else {
        $aPOST = array();
        foreach ($param as $key => $val) {
            $aPOST [] = $key . "=" . urlencode($val);
        }
        $strPOST = join("&", $aPOST);
    }

    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($oCurl, CURLOPT_POST, true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus ["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * CURLFile请求
 * @param $url
 * @param $data
 * @param array $header
 * @param $data_name
 * @return string
 */
public function http_upload_file($url, $data, $header, $data_name)
{
    $curl = curl_init();
    if (class_exists('CURLFile')) {
        $data = array($data_name => new \CURLFile(realpath($data)));//>=5.5
    } else {
        $data = array($data_name => '@' . realpath($data));//<=5.5
    }
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if (!empty($header)) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_HEADER, 0);//返回response头部信息
    }
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    $result = curl_exec($curl);

    curl_close($curl);

    if (intval($result["http_code"]) == 200) {
        return $result;
    } else {
        return false;
    }
}
Last modification:October 10th, 2020 at 02:54 pm
如果觉得我的文章对你有用,请随意赞赏