代码之家  ›  专栏  ›  技术社区  ›  Andrew G. Johnson

如何在php中访问一个restful api,它是密码投影的(我知道密码)

  •  0
  • Andrew G. Johnson  · 技术社区  · 15 年前

    例如,这里有一个url: https://rexms.net:32005/rexwapi/common/timeframes --如果我在浏览器中转到那个url,输入正确的用户名/密码,它就会向我吐出xml。问题是我需要通过php来访问它,所以我显然没有得到输入用户名/密码的提示。

    当前代码为:

    $timeframes_xml = simplexml_load_file(file_get_contents('https://rexms.net:32005/rexwapi/common/timeframes'));
    
    5 回复  |  直到 15 年前
        1
  •  6
  •   David Snabel-Caunt    15 年前

    我想你可以简单地使用

    https://username:password@rexms.net:32005/rexwapi/common/timeframes
    
        2
  •  4
  •   CoderDennis    15 年前

    你需要使用 curl 具有 curl_setopt 设置 CURLOPT_USERPWD 选择权。

        3
  •  2
  •   Darrel Miller    15 年前

    您需要设置http头授权。将该值设置为base64编码的

    Basic username:password
    

    这是假设服务器正在使用基本身份验证。在windows上,可以使用fiddler查看服务器请求的身份验证类型。

        4
  •  1
  •   Jim Walker    15 年前

    最好使用三个pear包来实现这一点:http/socket/net。使用http包,您可以轻松地向基本auth领域发出rest请求。

    更多信息请查看: How To: Making a PHP REST client to call REST resources

        5
  •  0
  •   Community CDub    8 年前

    找到我的答案 in another SO question

    function get_rex_xml($url)
    {
        $curl = curl_init($url);
        curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => TRUE,CURLOPT_USERPWD => 'username:password'));
        ob_start();
        $response_xml = curl_exec($curl);
        ob_end_clean();
        curl_close($curl);
        return $response_xml;
    }
    
    header('content-type:text/xml');
    echo get_rex_xml('https://rexms.net:32005/rexwapi/common/timeframes');