开发者

Making a HTTP GET request with HTTP-Basic authentication

开发者 https://www.devze.com 2023-04-12 14:02 出处:网络
I need to build a proxy for a Flash Player pro开发者_运维知识库ject I\'m working on. I simply need to make a HTTP GET request with HTTP-Basic authentication to another URL, and serve the response from

I need to build a proxy for a Flash Player pro开发者_运维知识库ject I'm working on. I simply need to make a HTTP GET request with HTTP-Basic authentication to another URL, and serve the response from PHP as if the PHP file was the original source. How can I do this?


Marc B did a great job of answering this question. I recently took his approach and wanted to share the resulting code.

<?PHP

$username = "some-username";
$password = "some-password";
$remote_url = 'http://www.somedomain.com/path/to/file';

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header' => "Authorization: Basic " . base64_encode("$username:$password")                 
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);

print($file);

?>

I hope that this is helpful to people!


Using file_get_contents() with a stream to specify the HTTP credentials, or use curl and the CURLOPT_USERPWD option.


I took @clone45's code and turned it into a series of functions somewhat like Python's requests interface (enough for my purposes) using only no external code. Maybe it can help someone else.

It handles:

  • basic auth
  • headers
  • GET Params

Usage:

$url = 'http://sweet-api.com/api';
$params = array('skip' => 0, 'top' => 5000);
$header = array('Content-Type' => 'application/json');
$header = addBasicAuth($header, getenv('USER'), getenv('PASS'));
$response = request("GET", $url, $header, $params);
print($response);

Definitions

function addBasicAuth($header, $username, $password) {
    $header['Authorization'] = 'Basic '.base64_encode("$username:$password");
    return $header;
}

// method should be "GET", "PUT", etc..
function request($method, $url, $header, $params) {
    $opts = array(
        'http' => array(
            'method' => $method,
        ),
    );

    // serialize the header if needed
    if (!empty($header)) {
        $header_str = '';
        foreach ($header as $key => $value) {
            $header_str .= "$key: $value\r\n";
        }
        $header_str .= "\r\n";
        $opts['http']['header'] = $header_str;
    }

    // serialize the params if there are any
    if (!empty($params)) {
        $params_array = array();
        foreach ($params as $key => $value) {
            $params_array[] = "$key=$value";
        }
        $url .= '?'.implode('&', $params_array);
    }

    $context = stream_context_create($opts);
    $data = file_get_contents($url, false, $context);
    return $data;
}


You really want to use php for that ?

a simple javascript script does it:

function login(username, password, url) {

  var http = getHTTPObject();

  http.open("get", url, false, username, password);
  http.send("");
  //alert ("HTTP status : "+http.status);

  if (http.status == 200) {
    //alert ("New window will be open");
    window.open(url, "My access", "width=200,height=200", "width=300,height=400,scrollbars=yes");
    //win.document.location = url;
  } else {
    alert("No access to the secured web site");
  }

}

function getHTTPObject() { 

  var xmlhttp = false;
  if (typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  } else {
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
    @end @*/
  }
  return xmlhttp;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号