Reputation: 1004
I have this parameters to download a XML file:
wget --http-user=user --http-password=pass http://www.example.com/file.xml
How I have to use that in php to open this xml file?
Upvotes: 41
Views: 189214
Reputation: 2880
To run wget command in PHP you have to do following steps :
1) Allow apache server to use wget command by adding it in sudoers list.
2) Check "exec" function enabled or exist in your PHP config.
3) Run "exec" command as root user i.e. sudo user
Below code sample as per ubuntu machine
#Add apache in sudoers list to use wget command
~$ sudo nano /etc/sudoers
#add below line in the sudoers file
www-data ALL=(ALL) NOPASSWD: /usr/bin/wget
##Now in PHP file run wget command as
exec("/usr/bin/sudo wget -P PATH_WHERE_WANT_TO_PLACE_FILE URL_OF_FILE");
Upvotes: 0
Reputation: 28589
wget
wget
is a linux command, not a PHP command, so to run this you woud need to use exec
, which is a PHP command for executing shell commands.
exec("wget --http-user=[user] --http-password=[pass] http://www.example.com/file.xml");
This can be useful if you are downloading a large file - and would like to monitor the progress, however when working with pages in which you are just interested in the content, there are simple functions for doing just that.
The exec
function is enabled by default, but may be disabled in some situations. The configuration options for this reside in your php.ini
, to enable, remove exec
from the disabled_functions
config string.
alternative
Using file_get_contents
we can retrieve the contents of the specified URL/URI. When you just need to read the file into a variable, this would be the perfect function to use as a replacement for curl - follow the URI syntax when building your URL.
// standard url
$content = file_get_contents("http://www.example.com/file.xml");
// or with basic auth
$content = file_get_contents("http://user:[email protected]/file.xml");
As noted by Sean the Bean
- you may also need to change allow_url_fopen
to true
in your php.ini to allow the use of a URL in this method, however, this should be true by default.
If you want to then store that file locally, there is a function file_put_contents
to write that into a file, combined with the previous, this could emulate a file download:
file_put_contents("local_file.xml", $content);
Upvotes: 61
Reputation: 31
<?php
function wget($address,$filename)
{
file_put_contents($filename,file_get_contents($address));
}
?>
use:
<?php
wget(URL, FileName);
?>
Upvotes: 1
Reputation:
I understand you want to open a xml file using php. That's called to parse a xml file. The best reference is here.
http://php.net/manual/de/function.xml-parse.php
Upvotes: 0
Reputation: 755
This method is only one class and doesn't require importing other libraries or reusing code.
Personally I use this script that I made a while ago. Located here but for those who don't want to click on that link you can view it below. It lets the developer use the static method HTTP::GET($url, $options)
to use the get method in curl while being able to pass through custom curl options. You can also use HTTP::POST($url, $options)
but I hardly use that method.
/**
* echo HTTP::POST('http://accounts.kbcomp.co',
* array(
* 'user_name'=>'[email protected]',
* 'user_password'=>'demo1234'
* )
* );
* OR
* echo HTTP::GET('http://api.austinkregel.com/colors/E64B3B/1');
*
*/
class HTTP{
public static function GET($url,Array $options=array()){
$ch = curl_init();
if(count($options>0)){
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);
return $json;
}
}
public static function POST($url, $postfields, $options = null){
$ch = curl_init();
$options = array(
CURLOPT_URL=>$url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => true
//CURLOPT_HTTPHEADER, array('Content-Type:application/json')
);
if(count($options>0)){
curl_setopt_array($ch, $options);
}
$json = curl_exec($ch);
curl_close($ch);
return $json;
}
}
Upvotes: 2
Reputation: 173522
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:[email protected]/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');
Upvotes: 39
Reputation: 54379
Shellwrap is great tool for using the command-line in PHP!
Your example can be done quite easy and readable:
use MrRio\ShellWrap as sh;
$xml = (string)sh::curl(['u' => 'user:pass'], 'http://example.com/file.xml');
Upvotes: 1
Reputation: 1458
If using Curl in php...
function disguise_curl($url)
{
$curl = curl_init();
// Setup headers - I used the same headers from Firefox version 2.0.0.6
// below was split up because php.net said the line was too long. :/
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$html = curl_exec($curl); // execute the curl command
curl_close($curl); // close the connection
return $html; // and finally, return $html
}
// uses the function and displays the text off the website
$text = disguise_curl($url);
echo $text;
?>
Upvotes: 2
Reputation: 18807
You can use curl
in order to both fetch the data, and be identified (for both "basic" and "digest" auth), without requiring extended permissions (like exec or allow_url_fopen).
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/file.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
$result = curl_exec($ch);
curl_close($ch);
Your result will then be stored in the $result
variable.
Upvotes: 11
Reputation: 1374
lots of methods available in php to read a file like exec, file_get_contents, curl and fopen but it depend on your requirement and file permission
Visit this file_get_contents vs cUrl
Basically file_get_contents for for you
$data = file_get_contents($file_url);
Upvotes: 2