note 111032 modified in function.stream-context-create by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:20:54 +0000 |
| Subject: | note 111032 modified in function.stream-context-create by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193135@lists.php.net to get a copy of this message | ||
I spent a good five hours trying to figure this out, so hopefully it will save someone else some
time.
When you are trying to download a file via ftp through an HTTP proxy note that the following will
not be enough:
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("ftp://anonymous:anonymous@ftp.example.org",false,$context);
?>
Your proxy will respond that authentication is required. You may scratch your head and think
"but I'm providing authentication!"
The issue is that the 'header' value is only applicable to http connections. So to
authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for
using on FTP.
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
),
'http' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("http://www.example.com",false,$context);
$s = file_get_contents("ftp://anonymous:anonymous@ftp.example.org",false,$context);
?>
It's a bit roundabout, but it works. Note that the 'header' val in the ftp array is
redundant, but I kept it in anyway.
--was--
I spent a good five hours trying to figure this out, so hopefully it will save someone else some
time.
When you are trying to download a file via ftp through an HTTP proxy note that the following will
not be enough:
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("ftp://anonymous:anonymous@ftp.mozilla.org",false,$context);
?>
Your proxy will respond that authentication is required. You may scratch your head and think
"but I'm providing authentication!"
The issue is that the 'header' value is only applicable to http connections. So to
authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for
using on FTP.
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
),
'http' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("http://www.google.ca",false,$context);
$s = file_get_contents("ftp://anonymous:anonymous@ftp.mozilla.org",false,$context);
?>
It's a bit roundabout, but it works. Note that the 'header' val in the ftp array is
redundant, but I kept it in anyway.
http://php.net/manual/en/function.stream-context-create.php