0

我在 Apache 服务器上启用了基本身份验证。服务器托管了一个我实现的 API,我想做的是从我的 PHP 脚本中调用这个 API。我已经弄清楚如何创建标题:

$user = 'my_name_api';
$pwd = 'xxxxxxxxx';
$auth_string = $user . ':' . $pwd;
$auth_b64 = base64_encode($auth_string);
$header   = 'Authorization: Basic ' . $auth_b64;

如何在我的 API 调用中包含 $header?我正在寻找 cURL 以外的东西,并且我没有使用 zend 等任何环境(看到 Zend 等的一些示例,但我没有使用其中任何一个)。

4

2 回答 2

1

你试过 fopen / file_get_contents 吗?

从这里开始:http: //php.net/manual/en/function.fopen.php

或在这里:http ://www.php.net/manual/en/function.file-get-contents.php

让我知道进展如何...

艾瑞特

于 2012-11-13T19:39:57.097 回答
1

如果您不需要 SSL 或代理支持,您可以使用file_get_contents()流上下文。流上下文可以包含 HTTP 标头:

$opts = array
(
  'http'=>array
  (
    'method' => "GET",
    'header' => "Authorization: ..."
  )
);
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com', false, $context);

更多关于这个here

于 2012-11-13T19:39:18.190 回答