note 111032 added to function.stream-context-create
| From: | BenJ | Date: | Tue, 08 Jan 2013 15:28:47 +0000 |
| Subject: | note 111032 added to function.stream-context-create | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-192840@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.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.
----
Server IP: 69.147.83.199
Probable Submitter: 74.216.249.70
----
Manual Page -- http://php.net/manual/en/function.stream-context-create.php
Edit -- https://master.php.net/note/edit/111032
Del: integrated -- https://master.php.net/note/delete/111032/integrated
Del: useless -- https://master.php.net/note/delete/111032/useless
Del: bad code -- https://master.php.net/note/delete/111032/bad+code
Del: spam -- https://master.php.net/note/delete/111032/spam
Del: non-english -- https://master.php.net/note/delete/111032/non-english
Del: in docs -- https://master.php.net/note/delete/111032/in+docs
Del: other reasons-- https://master.php.net/note/delete/111032
Reject -- https://master.php.net/note/reject/111032
Search -- https://master.php.net/manage/user-notes.php