0

I am beginner in php. I need download file from external server and save in my add folder. I have this code, but it's not working:

try {
        $url = "http://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip";

        exec('wget -O -v --debug import/data.zip '.$url.' 2>&1', $output);
        echo '<pre>';
        print_r($output);  // to see the response to your command

        echo 'Download complete';
    }
    catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }

When i run this code i have error:

Array
(
    [0] => DEBUG output created by Wget 1.21.2 on linux-gnu.
    [1] => 
    [2] => Reading HSTS entries from /var/www/.wget-hsts
    [3] => URI encoding = 'ANSI_X3.4-1968'
    [4] => converted 'http://import/data.zip' (ANSI_X3.4-1968) -> 'http://import/data.zip' (UTF-8)
    [5] => --2023-04-14 13:49:41--  http://import/data.zip
    [6] => Resolving import (import)... failed: No address associated with hostname.
    [7] => wget: unable to resolve host address 'import'
    [8] => URI encoding = 'ANSI_X3.4-1968'
    [9] => converted 'http://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip' (ANSI_X3.4-1968) -> 'http://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip' (UTF-8)
    [10] => --2023-04-14 13:49:42--  http://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip
    [11] => Resolving opendata.geoportal.gov.pl (opendata.geoportal.gov.pl)... 91.223.135.201
    [12] => Caching opendata.geoportal.gov.pl => 91.223.135.201
    [13] => Connecting to opendata.geoportal.gov.pl (opendata.geoportal.gov.pl)|91.223.135.201|:80... connected.
    [14] => Created socket 4.
    [15] => Releasing 0x00005635f7804e20 (new refcount 1).
    [16] => 
    [17] => ---request begin---
    [18] => GET /prg/adresy/PRG-punkty_adresowe.zip HTTP/1.1
    [19] => Host: opendata.geoportal.gov.pl
    [20] => User-Agent: Wget/1.21.2
    [21] => Accept: */*
    [22] => Accept-Encoding: identity
    [23] => Connection: Keep-Alive
    [24] => 
    [25] => ---request end---
    [26] => HTTP request sent, awaiting response...
    [27] => ---response begin---
    [28] => HTTP/1.0 302 Moved Temporarily
    [29] => Location: https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip
    [30] => Connection: Keep-Alive
    [31] => Content-Length: 0
    [32] => 
    [33] => ---response end---
    [34] => 302 Moved Temporarily
    [35] => Registered socket 4 for persistent reuse.
    [36] => Location: https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip [following]
    [37] => ] done.
    [38] => URI content encoding = None
    [39] => converted 'https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip' (ANSI_X3.4-1968) -> 'https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip' (UTF-8)
    [40] => --2023-04-14 13:49:42--  https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip
    [41] => Found opendata.geoportal.gov.pl in host_name_addresses_map (0x5635f7804e20)
    [42] => Connecting to opendata.geoportal.gov.pl (opendata.geoportal.gov.pl)|91.223.135.201|:443... connected.
    [43] => Created socket 5.
    [44] => Releasing 0x00005635f7804e20 (new refcount 1).
    [45] => Initiating SSL handshake.
    [46] => SSL handshake failed.
    [47] => OpenSSL: error:0A000152:SSL routines::unsafe legacy renegotiation disabled
    [48] => Closed fd 5
    [49] => Unable to establish SSL connection.
)

and file is not download :( I have php 8.1 and apache. File to download file: http://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip

How can i repair it?

Maybe i must remove function exec and use something others?

Please help me

quatrol
  • 39
  • 4
  • 1
    For starters, you should put the HTTPS version of the URL in your code to begin with - that spares you from getting _redirected_ to the HTTPS version. – CBroe Apr 14 '23 at 12:04
  • The error message reads at the bottom *"SSL handshake failed."* *"OpenSSL: error:0A000152:SSL routines::unsafe legacy renegotiation disabled"*. It seems the website is using an older unsafe SSL negotation? Have you searched for that error description yet? – Peter Krebs Apr 14 '23 at 12:05
  • 1
    Does this answer your question? [Download File to server from URL](https://stackoverflow.com/questions/3938534/download-file-to-server-from-url) – Justinas Apr 14 '23 at 12:06
  • _"OpenSSL: error:0A000152:SSL routines::unsafe legacy renegotiation disabled"_ - issue similar to https://stackoverflow.com/q/71603314/1427878 then, but I am not sure if you can employ the same solution here (probably worth a try though.) Or consider alternatives to using wget, such as `file_get_contents` (would need allow_url_fopen to be set to true in your PHP config), or PHP cURL. – CBroe Apr 14 '23 at 12:06
  • I try with https - not working :( – quatrol Apr 14 '23 at 12:26
  • https://stackoverflow.com/questions/3938534/download-file-to-server-from-url - i try this, not working :( – quatrol Apr 14 '23 at 12:31
  • "not working" how? Same error? Or a diffferent one? You can [edit] your question with your latest attempt and the results. – ADyson Apr 14 '23 at 12:33
  • it's download empty file or return error: OpenSSL: error:0A000152:SSL routines::unsafe legacy renegotiation disabled – quatrol Apr 14 '23 at 12:49

1 Answers1

1
$url = "https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip";
$destination = "import/data.zip"; // replace with your desired directory path

$file = file_get_contents($url);
file_put_contents($destination, $file);