2

次のコードを使用して、別のページから情報を読み込むスクリプトがあります。

    $creds = 'user:password';
$base64 = base64_encode($creds);

$base_url = 'http://example.com/page.html';
$data = file_get_contents($base_url.$path, false, $auth);

これは、1 つのサーバー (Ubuntu を使用する私のローカル サーバー) からは正常に動作しますが、外部サーバーから実行すると、次のヘッダーが表示されます。

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.7a DAV/2 SVN/1.6.6 PHP/5.3.14   JRun/4.0 Server at ww2.rch.org.au Port 80</address>
</body></html>

ここで特に探すべきものはありますか?役立つ場合は、両方のサーバーで allow_url_fopen を true に設定しています。

編集:申し訳ありませんが、削除が必要な他のコードがいくつかありました。$auth は次のとおりです。

$auth = stream_context_create(array(
  'http'    => array(
  'header'  => 'Authorization: Basic '.$base64
  )
));

$auth は、承認ヘッダーを含むストリーム コンテキストです。

4

1 に答える 1

3

関数のドキュメントによると、「auth」パラメーターはありません。

HTTP クレデンシャルは通常、次の形式で追加されます。

http://username:password@example.com/page.html
于 2013-02-18T10:16:02.777 に答える