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
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;
?>
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
<?php
function wget($address,$filename)
{
file_put_contents($filename,file_get_contents($address));
}
?>
use:
<?php
wget(URL, FileName);
?>
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.
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');
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'=>'demo@example.com',
* '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;
}
}