Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a file using copy through an ntlm proxy on php?

I have a qemu linux virtual machine and I'm trying to install composer on it using the commands on the composer page. I'm on a windows network accessing the internet through a proxy that uses ntlm, so I use cntlm to authenticate linux and other programs on my PC (thanks to the people that created cntlm). I added the context to the copy command needed to access the proxy but it doesn't work.

This are the command used so far:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php', stream_context_create(['https' => ['proxy' => 'http://10.0.2.2:3128/']]));"

# a variant
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php', stream_context_create(['https' => ['proxy' => 'tcp://10.0.2.2:3128/']]));"

The answer is:

PHP Warning: copy(https://getcomposer.org/installer): failed to open stream: Connection timed out in Command line code on line 1

Download the file using wget works fine.

$ env | grep "proxy"
https_proxy=http://10.0.2.2:3128/
http_proxy=http://10.0.2.2:3128/
$ wget -O composer-setup.php https://getcomposer.org/installer
--2017-09-XX XX:XX:XX-- https://getcomposer.org/installer
Connecting to 10.0.2.2:3128 ... conected
Request send ... 200 OK
... etc
2017-09-XX XX:XX:XX (XX KB/s) - composer-setup.php saved [305728/305728]

The sites used as reference:

  • https://getcomposer.org/download/
  • http://php.net/manual/en/function.copy.php
  • http://php.net/manual/en/function.stream-context-create.php
  • http://php.net/manual/en/context.http.php

I know there is a manual way to install composer, but I'm just a bit curious, How can make this work?

like image 574
programingfrik Avatar asked Sep 11 '17 15:09

programingfrik


2 Answers

There is no https option for stream_context_create(), there is only http which affects both http and https protocols:

Context options for http:// and https:// transports.
https://php.net/manual/en/context.http.php

So you should probably use something like this:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php', stream_context_create(['http' => ['proxy' => 'tcp://10.0.2.2:3128']]));"
like image 177
rob006 Avatar answered Nov 12 '22 07:11

rob006


You don't necessarily have to do the download with PHP.

Since the wget download works you can:

wget -O composer-setup.php https://getcomposer.org/installer
export EXPECTED_HASH=$(wget -q -O - https://composer.github.io/installer.sig)
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$EXPECTED_HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
like image 37
Steve Buzonas Avatar answered Nov 12 '22 08:11

Steve Buzonas