【问题标题】:How to use basic authorization in PHP curl如何在 PHP curl 中使用基本授权
【发布时间】:2013-12-02 13:49:50
【问题描述】:

我在使用基本授权的 PHP curl 请求时遇到问题。

这里是命令行 curl:

curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"

我尝试通过以下方式设置 curl 标题,但它不起作用

Authorization: Basic id:api_key
or 
Authorization: Basic {id}:{api_key}

我得到响应“请求中的身份验证参数丢失或无效”,但我使用了正确的 id 和 api_key,它们在命令行 curl 中工作(我测试过)

请帮帮我。

【问题讨论】:

    标签: php curl authorization


    【解决方案1】:

    试试下面的代码:

    $username='ABC';
    $password='XYZ';
    $URL='<URL>';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    $result=curl_exec ($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
    curl_close ($ch);
    

    【讨论】:

    • 不错的一个!我有curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');,但这不起作用。用了你的CURLAUTH_ANY,我们很成功!
    • 我还能够删除CURLOPT_HTTPAUTH 行并使其正常工作。
    • @JoshPinter,您使用的是字符串 'CURLAUTH_BASIC' 而不是常量 CURLAUTH_BASIC
    • 这对我有用curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Basic ".base64_encode($this-&gt;username.":".$this-&gt;password), ]);
    • curl_getinfo() 不应该在 curl_exec() 之后吗?
    【解决方案2】:

    你可以试试这个,

     $ch = curl_init($url);
     ...
     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  
     ...
    

    参考号:http://php.net/manual/en/function.curl-setopt.php

    【讨论】:

    • 这实际上对我有用,而不是 curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    【解决方案3】:
    $headers = array(
        'Authorization: Basic '. base64_encode($username.':'.$password),
    );
    ...
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    

    【讨论】:

    • 当用三个现有答案回答一个六年前的问题时,重要的是要解释您的答案所针对的问题的哪些新方面。仅代码答案几乎总是可以通过添加解释来改进。
    • @JasonAller 我已经测试了所有这些但没有工作。有一条评论喜欢我的回答,但我认为这个解决方案必须是一个答案。
    【解决方案4】:

    传递标题的简单方法

    function get_data($url) {
    
    $ch = curl_init();
    $timeout = 5;
    $username = 'c4f727b9646045e58508b20ac08229e6';        // Put Username 
    $password = '';                                        // Put Password
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");    // Add This Line
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    $url = "https://storage.scrapinghub.com/items/397187/2/127";
    $data = get_data($url);
    echo '<pre>';`print_r($data_json);`die;    // For Print Value
    

    Check My JSON Value

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 2019-10-29
      • 2014-01-15
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多