当前位置:Gxlcms > PHP教程 > PHPCurl函数,支持POSTGET_PHP教程

PHPCurl函数,支持POSTGET_PHP教程

时间:2021-07-01 10:21:17 帮助过:3人阅读

PHP Curl 函数,支持POST GET


/**
 * @author       default7
 * @param        $url
 * @param string $method
 * @param array  $postData
 *
 * @return mixed|null|string
 */
function getHttpContent($url, $method = 'GET', $postData = array())
{
    $data = '';

    if (!empty($url)) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
            if (strtoupper($method) == 'POST') {
                $curlPost = http_build_query($postData);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            }
            $data = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $data = null;
        }
    }

    return $data;
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/871188.htmlTechArticlePHP Curl 函数,支持POST GET /** * @author default7 * @param $url * @param string $method * @param array $postData * * @return mixed|null|string */function getHttpContent($url,...

人气教程排行