【问题标题】:how to connect API endpoint URL in php using api key and header [duplicate]如何使用 api 密钥和标头连接 php 中的 API 端点 URL [重复]
【发布时间】:2023-02-08 13:51:26
【问题描述】:

我有 api 密钥和标头,我可以从邮递员连接。

在 postman 中,Authorization-> type-apikey 在 Headers 中,我使用了 Key 和 value。

我想在 php 中连接 api。有人可以帮忙吗?

【问题讨论】:

    标签: php api


    【解决方案1】:

    您可以为此目的只使用 cURL 库吗?为此,使用 cURL 在 PHP 中发送带有 API 密钥和标头的 GET 请求:

    <?php
    
    $header = array(
        'Authorization: ApiKey ' . $apiKey,
        'Content-Type: application/json'
    );
    
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => $endpointUrl,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => $header,
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    
    echo $response;
    
    ?>
    

    为 $apiKey 和 $endpointUrl 输入你的实际值

    希望能帮助到你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2021-08-01
      • 2020-11-10
      • 1970-01-01
      • 2019-05-02
      相关资源
      最近更新 更多