美文网首页
php post 设置header json传参

php post 设置header json传参

作者: 第五季资源 | 来源:发表于2020-04-28 11:35 被阅读0次

    public function urlGetContent(url,params = null, method = 'GET',json = false)
    {
    ch = curl_init(); curl_setopt(ch, CURLOPT_URL, url); curl_setopt(ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt(ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt(ch, CURLOPT_HEADER, 0);

        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            if ($json && is_array($params)) {
                $params = json_encode($params);
            }
            if (is_string($params) || is_array($params)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            } else {
                // error_log("Argument \$params should be an array of parameters or (if you want to send raw data) a string");
                return false;
            }
    
            if ($json) { //发送JSON数据
                curl_setopt($ch, CURLOPT_HTTPHEADER,
                    array(
                        'Content-Type: application/json; charset=utf-8',
                        'Content-Length:' . strlen($params),
                      
            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    相关文章

      网友评论

          本文标题:php post 设置header json传参

          本文链接:https://www.haomeiwen.com/subject/ftkvwhtx.html