The HTTP context options are laid out here: http://www.php.net/manual/en/context.http.php
header
オプションは文字列なので、@Mobは配列ではなく \ r \ n
と文字列連結を使用する必要があると言います。しかし、 user_agent
は有効なキーなので、代わりに使用することができます。
私は、 $ auth
変数の内容が Authorization:blah
の行に沿ったものであると推測しています。
The below code is a working example. Note that I've changed your fpassthru()
(which outputs the content to the browser, and does not store it to $result
) to a fread()
loop. Alternatively you could have wrapped the fpassthru()
call with ob_start();
and $result = ob_get_clean();
<?php
class RESTClient {
const USER_AGENT = 'bob';
}
$url = 'http://www.example.com/';
$username = "fish";
$password = "paste";
$b64 = base64_encode("$username:$password");
$auth = "Authorization: Basic $b64";
$opts = array (
'http' => array (
'method' => "GET",
'header' => $auth,
'user_agent' => RESTClient :: USER_AGENT,
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
$result = "";
while ($str = fread($fp,1024)) {
$result .= $str;
}
fclose($fp);
echo $result;