Asked  7 Months ago    Answers:  5   Viewed   40 times

The file which I'm trying to download from my PHP script is this one: 

But I can't do it using neither file_get_contents() nor cURL. I'm getting the error Object reference not set to an instance of an object.

Any idea how to do it?

Thanks a lot, Pablo.

Updated to add the code:

$url = "";
$simple = simplexml_load_file(file_get_contents($url));
foreach ($simple->farmacia as $farmacia)

And the solution thanks to @Gordon:

$url = "";
$file = file_get_contents($url, FALSE, stream_context_create(array('http' => array('user_agent' => 'php' ))));
$simple = simplexml_load_string($file);



You dont need cURL, nor file_get_contents to load XML into any of PHP's DOM Based XML parsers.

However, in your particular case, the issue seems to be that the server expects a user agent in the http request. If the user agent is not set in your php.ini, you can use the libxml functions and provide it as a stream context:

            'http' => array(
                'user_agent' => 'php'            

$dom = new DOMDocument;
echo $dom->saveXml();

Live Demo

If you dont want to parse the XML file afterwards, you can use file_get_contents as well. You can pass the stream context as the third argument:

echo file_get_contents(
            'http' => array(
                'user_agent' => 'php'            

Live Demo

Wednesday, March 31, 2021
answered 7 Months ago

I think curl is more secure because if you're working with remote file with file_get_contents() you need to enable ‘allow_url_fopen’

reference :

And continuing discussion from the comments in the question, yes cURL give you more option and if you want to check more you can see it in the documentation here
For file_get_contents() it just a simple GET request.

Saturday, May 29, 2021
answered 5 Months ago

You should use <base> to specify a base url for all relative links:

If you curl then add a base tag in your echoed output of ''. This should technically be in the <head>, but this will work:

echo '<base href="" />';
echo $html;

Live example w <base> is broken w/o <base>

Saturday, May 29, 2021
answered 5 Months ago

The answers.All scripts should be saved with .bat/.cmd extensions and can be used directly as batch scripts.

  1. Certutuil (for some reasons in the newest win10 builds this is recognized as trojan thread ):

    certutil.exe -urlcache -split -f ""

for easiness a macro can be used:

set "download=certutil.exe -urlcache -split -f"
%download% ""

CertUtil command can be abused to download a file from internet.Available by default in windows since Vista.For WinXP Server 2003 Administration Tools are needed.

  1. Bitsadmin :

simplest possible way to use it

bitsadmin /transfer myDownloadJob /download /priority normal http://downloadsrv/

with macro:

set "dnld=bitsadmin /transfer myDownloadJob /download /priority normal"
%dnld% ""

or with bitsDownloader.bat

call bitsDownloader.bat ""
  1. winhhtpjs.bat is a command line http client that uses WinHttpRequest .It can perform whole range of http (POST,DELETE,..) requests and can be used for downloading of files too (not too big files).Also custom headers can be added.
call winhhtpjs.bat "" -saveTo "" 
  1. XMLHTTPDownloadJS.bat is bat file that uses MSXML2.XMLHTTP object to download a file . Does not offer so rich options as winhhtpjs.bat , though is still an option.

    call XMLHTTPDownloadJS.bat ""

  2. WebClientDownload.bat uses the .NET System.Net.WebClient class. It creates a small exe file in order to selfcompile itself and requires an installed a .net framework. As the class is presented in the very earlier versions of .net it is backward compatible enough

call webclientDownload.bat ""
  1. With the latest builds of windows 10 we have the CURL command ,though this is not so backward compatible option. Mind that only the newest versions of windows has CURL installed by default.

    curl "" --output

Saturday, June 5, 2021
answered 5 Months ago

Try this (not sure if it's the best way, but it works):

find . -type f | perl -ne 'print $1 if m/.([^./]+)$/' | sort -u

It work as following:

  • Find all files from current folder
  • Prints extension of files if any
  • Make a unique sorted list
Friday, June 11, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :