【问题标题】:GET request with authentication带有身份验证的 GET 请求
【发布时间】:2015-09-21 11:40:02
【问题描述】:

我有可以使用身份验证将 JSON 发布到 API 的代码。我需要使用身份验证 GET,但大多数示例不提供此信息。

发布

public function putTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";
    $json_string = "[]";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Content-type: application/json" ),
        CURLOPT_POSTFIELDS     => $json_string
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}

GET(不工作)

public function getTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Content-type: application/json" ),
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}

我的 GET 中缺少什么?我也想保持这种风格而不是使用curl_setopt

编辑: 作为参考,这是可用的命令行版本

curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget"

【问题讨论】:

    标签: php json rest


    【解决方案1】:

    你需要使用CURLOPT_HTTPAUTH

    使用 CURLOPT_HTTPAUTH 指定 HTTP 的身份验证方法 基于连接

    http://curl.haxx.se/libcurl/c/CURLOPT_USERPWD.html

    【讨论】:

    • 试过了,我想我还缺少别的东西
    【解决方案2】:

    你需要添加:

    curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
    

    一个例子:

    public function call(array $data)
    {
        $payload = json_encode($data['payload']);
    
        // Initialise the session
        $curl = curl_init();
    
        // Set API URL
        curl_setopt($curl, CURLOPT_URL, $data['url']);
        // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        // Include the header in the output
        curl_setopt($curl, CURLOPT_HEADER, true);
        // Authentication
        curl_setopt($curl_handle, CURLOPT_USERPWD, 'your_username:and_password');
        // Set request method
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $data['method']);
    
        if ($data['method'] == 'POST') {
            // Do a regular HTTP POST
            curl_setopt($curl, CURLOPT_POST, true);
        }
    
        if ($data['method'] == 'POST' || $data['method'] == 'PUT') {
            // The full data to post in a HTTP POST operation
            curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($payload)
            ));
        }
    
        // Execute the request
        $output = curl_exec($curl);
        // Close curl resource to free up system resources
        curl_close($curl);
    
        // If connection failed
        if (! $output) {
            return 'Curl connection failure.';
        }
    
        // Output the profile information, includes the header
        return $output;
    }
    

    【讨论】:

      【解决方案3】:

      所以 POST 需要 CURLOPT_HTTPHEADER => array( "Content-type: application/json" ), 而 GET 需要 CURLOPT_HTTPHEADER => array( "Accept: application/json" ), 现在阅读它是有意义的。

      public function getTest() {
          $username    = "user";
          $password    = "pass";
          $json_url    = "http://api.of.site.com";
      
          $ch      = curl_init( $json_url );
          $options = array(
              CURLOPT_SSL_VERIFYPEER => false,
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_USERPWD        => "{$username}:{$password}",
              CURLOPT_HTTPHEADER     => array( "Accept: application/json" ),
          );
          curl_setopt_array( $ch, $options );
      
          $result = curl_exec( $ch );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-05
        • 2012-11-29
        • 2017-06-01
        • 1970-01-01
        • 2014-02-01
        • 2020-08-25
        • 2020-07-10
        • 2016-03-15
        相关资源
        最近更新 更多