How to Use Wget in PHP

How to use wget in php?

If the aim is to just load the contents inside your application, you don't even need to use wget:

$xmlData = file_get_contents('http://user:pass@example.com/file.xml');

Note that this function will not work if allow_url_fopen is disabled (it's enabled by default) inside either php.ini or the web server configuration (e.g. httpd.conf).

If your host explicitly disables it or if you're writing a library, it's advisable to either use cURL or a library that abstracts the functionality, such as Guzzle.

use GuzzleHttp\Client;

$client = new Client([
'base_url' => 'http://example.com',
'defaults' => [
'auth' => ['user', 'pass'],
]]);

$xmlData = $client->get('/file.xml');

Downloading using wget in php

Before loading you can check headers (you'll have to download them though). I use curl - not wget. Here's an example:


$ curl --head http://img.yandex.net/i/www/logo.png

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 16 Jun 2012 09:46:36 GMT
Content-Type: image/png
Content-Length: 3729
Last-Modified: Mon, 26 Apr 2010 08:00:35 GMT
Connection: keep-alive
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Accept-Ranges: bytes

Content-Type and Content-Length should normally indicate that the image is ok

PHP: Save the file returned by wget with file_put_content

Its because exec doesn't return the whole data. Take a look at the documentation
https://www.php.net/manual/en/function.exec.php :

Return Values: The last line from the result of the command.

But shell_exec (or just backticks) returns whole data: https://www.php.net/manual/en/function.shell-exec.php .

Example:

<?php

$url = 'https://file-examples-com.github.io/uploads/2017/02/zip_5MB.zip';

$content = exec("wget -qO- $url");

var_dump(strlen($content));

$content2 = shell_exec("wget -qO- $url");

var_dump(strlen($content2));

file_put_contents('1.zip', $content);
file_put_contents('2.zip', $content2);

Output:

int(208)
int(5452018)

2.zip works (all 5MB data), 1.zip obviously not (just some bytes from the end).

So don't treat exec's return value as the whole output of the command.

How does the browser allow downloading the php file while wget doesn't? [MediaWiki installation]

your browser has cookies, wget doesn't, it's almost certainly a file protected by cookies, only those with the correct (authentication?) cookies can access the file, wget can not. in chrome open the developer console, navigate to the Network tab, download the file in chrome, find the request in the network tab, right click on the tab and press "copy as curl", and you'll see what the request looks like with cookies, it'll look more like:

curl 'https://stackoverflow.com/posts/validate-body' -H 'cookie: prov=5fad00f3-5ed3-bd3b-3a8a; _ga=GA1.2.20207544.1508821; sgt=id=e366-9d13-4df2-84de-2042; _gid=GA1.2.129666.1538138077; acct=t=Jyl74nJBTyCIYQq5mc2sf&s=StN3CVV2B5Opj051ywy7' -H 'origin: https://stackoverflow.com' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' -H 'user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'accept: /' -H 'referer: https://stackoverflow.com/' -H 'authority: stackoverflow.com' -H 'x-requested-with: XMLHttpRequest' --data $'body=your+browser+has+cookies%2C+wge&oldBody=&isQuestion=false' --compressed

  • and if you run that command in bash, you'll probably be able to download the file from the terminal.

wget its not working when executing with php

Thanks to Mike Q I figure out why wget was not working. It turn out it was a permission issue. I just had to change the folder owner to www-data which in my case was running the apache2 server and executing the php script, because that the folder was owner by root user. To find out which user is running apache2 service (in 99% it is www-data) you can check this link or just execute this script in php

echo shell_exec('whoami');

after that just go to the main folder and just change the rights with this command in the terminal

$ sudo chown -R www-data:www-data [folder-name]

Downloading PHP files from open directory using wget gives empty/blank PHP files

view-source:https://www.isuperman.tw/wp-content/plugins/automatewoo-referrals/automatewoo-referrals.php

nope the files are empty, doesnt matter whats the content of the files.. if you use wget to download a file, wget simulates A browser and get the parsed php content from the server...

and these seems to be empty

if you want to download the files with php, use ftp or the server must not parse these files and deliver its raw content

Is it possible to download PHP script from a web page with wget?

No, and thank goodness for that. The server is completely in control of how it responds to your HTTP requests.

Strictly speaking, you can't tell whether it's PHP on the other end of the wire in the first place.

wget not working when called from exec() or shell_exec()

I had to specify a path to wget. New command:

exec("/usr/local/bin/wget google.com", $array);


Related Topics



Leave a reply



Submit