Pobieranie pliku z zewnętrznego serwera

0

Hej,
Chciałbym pobrać plik z zewnętrznego serwera i zapisać go u siebie.
Mam taki kod:

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";
    }

Response:

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.
)

Mam php 8.1

Plik się nie pobiera :(

Wie ktoś może jak naprawić ten problem?

0

Masz problem z uściskiem ręki jak w logach.
Możesz dodać testowo parametr --no-check-certificate, ale tylko testowo bo rezygnujesz wtedy z weryfikacji certyfikatu a to kwestia bezpieczeństwa.
Nie możesz użyć zwykłego file_get_contentes https://www.php.net/manual/en/function.file-get-contents.php
Jak nie to może CURL i tam podajesz sobie parametry i certyfikat jeśli potrzeba.

0

--no-check-certificate - w dalszym ciągu OpenSSL: errorSSL routines::unsafe legacy renegotiation disabled

To zrób to z kodu będzie bardziej po bożemu niz z PHP wołać CMD i Wget.

0

U mnie działa:

<?php
$fileToGetUrl = "https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip";
$stringWithFileContent = file_get_contents($fileToGetUrl);

Testowane w CMD z memory_limit=4G bo plik duży. Reszta zależy od Ciebie, co chcesz z tym plikiem dalej robić.

0

kurczę, ustawiłem memory limit na 6GB - sprawdziłem w phpinfo() i jest ok.

Mam kod:

$fileToGetUrl = "https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip";
$stringWithFileContent = file_get_contents($fileToGetUrl);
echo '<pre>';
print_r($stringWithFileContent);  // to see the response to your command
echo '</pre>';

Odpalam z konsoli:
root:/var/www/html/ php multi_import.php
PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
errorSSL routines::unsafe legacy renegotiation disabled in /var/www/html/multi_import.php on line 12
PHP Warning: file_get_contents(): Failed to enable crypto in /var/www/html/multi_import.php on line 12
PHP Warning: file_get_contents(https://opendata.geoportal.gov.pl/prg/adresy/PRG-punkty_adresowe.zip): Failed to open stream: operation failed in /var/www/html/multi_import.php on line 12

0

A masz w PHP OpenSSL zainstalowany w rozszerzeniach?
Sprawdź w phpinfo() lub przez CMD php -m

Zobacz na innym komputerze i ewentualnie testowo:
https://support.axway.com/kb/182365/language/en

0

https://ibb.co/ZJL5cYz - open ssl mam :(

0

jaki system operacyjny ? może musisz zaktualizować certyfikaty ? (pakiet ca-certificates)
Albo jak system nowy (Ubuntu 22 itp) to musisz wymusić metodę legacy

1

Mam Ubuntu 22.04. Pomogło dodanie w pliku:

/etc/ssl/openssl.cnf

konfiga:

openssl_conf = openssl_init
....
Options = UnsafeLegacyRenegotiation

1 użytkowników online, w tym zalogowanych: 0, gości: 1