php post 设置header json传参

public function urlGetContent(params = null, json = false)
{
ch, CURLOPT_URL, ch, CURLOPT_SSL_VERIFYPEER, 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传参)