PHP如何在curl中使用承载令牌获取请求标头?

问题描述:

下面是有效的c#获取请求

below is a working c# get request

public HttpResponseMessage ExecuteEndPoint(string endpoint,string accessTocken) {
            HttpResponseMessage responseMessage = new HttpResponseMessage();
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessTocken);
                responseMessage = client.GetAsync(endpoint).Result;
            }
            return responseMessage;
        }

我想使用curl在php中执行相同的请求,这是我尝试过的

I would like to do the same request in php using curl, below is what i have tried

$request_headers=array();
     $request_headers[]='Bearer: '.$access_token;
    //$request_headers[]='Content-Length:150';
    $handle1=curl_init();
    $api_url='here my api get url';


    curl_setopt_array(
        $handle1,
        array(
                CURLOPT_URL=>$api_url,
                CURLOPT_POST=>false,
                CURLOPT_RETURNTRANSFER=>true,
                CURLOPT_HTTPHEADER=>$request_headers,
                CURLOPT_SSL_VERIFYPEER=>false,
                CURLOPT_HEADER=>true,
                CURLOPT_TIMEOUT=>-1
        )
    );

    $data=curl_exec($handle1);
    echo serialize($data);

好像api没有收到access_token.任何帮助表示赞赏.谢谢

Looks like the api is not receiving access_token. Any help is appreciated. Thanks

您还需要指定授权" ...

You also need to specify "Authorization"...

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer ".$access_token
));