美文网首页
PHP curl 请求中添加 Authorization tok

PHP curl 请求中添加 Authorization tok

作者: Mracale | 来源:发表于2021-05-10 17:10 被阅读0次

PHP curl 请求中添加 Authorization token ;

经过测试用一下方式设置 header 参数,可以成功获取数据

$accessToken = "6666666666669999999999";
$headers[]  =  "Accept:application/json";
$headers[]  =  "Authorization: Bearer ". $accessToken;

curl 参数设置如下:

if( !empty($headers) ){
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
}

curl请求方法

function curl_post($url, $data ,$aHeader=[]) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return false;
    }else{
        return $tmpInfo;
    }
}

相关文章

网友评论

      本文标题:PHP curl 请求中添加 Authorization tok

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