Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用HTTP基本身份验证发出HTTP GET请求_Php_Http_Basic Authentication - Fatal编程技术网

Php 使用HTTP基本身份验证发出HTTP GET请求

Php 使用HTTP基本身份验证发出HTTP GET请求,php,http,basic-authentication,Php,Http,Basic Authentication,我需要为我正在进行的Flash Player项目构建一个代理。我只需要向另一个URL发出一个带有HTTP基本身份验证的HTTP GET请求,并像PHP文件是原始源一样提供来自PHP的响应。如何执行此操作?使用with a来指定HTTP凭据,或者使用和选项。您真的要使用php吗 一个简单的javascript脚本可以实现这一点: function login(username, password, url) { var http = getHTTPObject(); http.open

我需要为我正在进行的Flash Player项目构建一个代理。我只需要向另一个URL发出一个带有HTTP基本身份验证的HTTP GET请求,并像PHP文件是原始源一样提供来自PHP的响应。如何执行此操作?

使用with a来指定HTTP凭据,或者使用和选项。

您真的要使用php吗

一个简单的javascript脚本可以实现这一点:

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;
}

马克B在回答这个问题上做得很好。我最近采用了他的方法,并希望与大家分享结果代码

<?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);

?>


我希望这对人们有帮助

我把@clone45的代码转换成一系列函数 像Python的 接口(对我来说足够了),只使用外部代码。也许它可以帮助其他人

它处理:

  • 基本授权
  • 标题
  • 获取参数
用法: 定义
您需要一个全PHP的解决方案,还是允许您对curl进行外部调用?如果它非常容易获得,我可能会使用
curl
。我希望我的脚本能够在尽可能多的机器上“正常工作”。如果不可用,则什么都没有,尽管您当然需要为每个目标平台操作系统重新编译源代码。我看到使用curl的主要优点是:1)支持复杂的东西(如带有auth的HTTP代理和客户端证书)“开箱即用”,2)诊断(帮助您找出某些HTTP事务失败的原因)。+1-还要注意,如果您没有流支持(PHP4)或curl扩展不可用,您可以使用
http://user:pass@domain.tld/file
URL语法,带有
file\u get\u contents()
,它将提供HTTP基本的身份验证功能。@DaveRandom更好,那太棒了。你能用PHP举例说明怎么做吗,我有点卡住了,我尝试了几种不同的方法,但都没有成功。你能更具体地说明你尝试了什么吗?有一些没有HTTP基本身份验证的示例-这些示例适用于您吗?如果是这样,并且由于某种原因添加auth前缀失败,那么您可能会从@DaveRandom得到一个提示,将凭据放在URL中的解决方案效果很好,除非发生从http到https的重定向。在这种情况下,PHP将请求的凭据“释放”到https URL。但是,如果它们在上下文中,它们仍然会被使用。正在进行一些旨在跨站点共享的工作,因此没有外部JavaScript可以工作,不幸的是,需要一个托管的全PHP解决方案。太好了,谢谢。如果回复带有链接怎么办?单击响应页面上的链接时,如何使用链接url调用请求函数?@Sadik如果响应来自链接,则需要从您获得的字符串中解析它们。例如,如果是JSON,你可以使用
JSON\u decode
函数。不,他回答这个问题做得很差。你做得很好。(计票结果与我一致!)
$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);
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;
}