Asked  2 Years ago    Answers:  5   Viewed   177 times

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

http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml 

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 = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
$simple = simplexml_load_file(file_get_contents($url));
foreach ($simple->farmacia as $farmacia)
{
    var_dump($farmacia);
}

And the solution thanks to @Gordon:

$url = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
$file = file_get_contents($url, FALSE, stream_context_create(array('http' => array('user_agent' => 'php' ))));
$simple = simplexml_load_string($file);

 Answers

3

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:

libxml_set_streams_context(
    stream_context_create(
        array(
            'http' => array(
                'user_agent' => 'php'            
            )
        )
    )
);

$dom = new DOMDocument;
$dom->load('http://www.navarra.es/app…/Farmacias.xml');
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://www.navarra.es/apps…/Farmacias.xml',
    FALSE,
    stream_context_create(
        array(
            'http' => array(
                'user_agent' => 'php'            
            )
        )
    )
);

Live Demo

Tuesday, November 29, 2022
2

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 :
http://25labs.com/alternative-for-file_get_contents-using-curl/
http://phpsec.org/projects/phpsecinfo/tests/allow_url_fopen.html

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.

Wednesday, September 21, 2022
3

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

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

echo '<base href="http://example.com/" />';
echo $html;

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

Saturday, September 3, 2022
 
ians
 
1

Bundle version and saving it in user defaults.

EDIT:

There are three things to note here.

  1. Bundle version: This is the version of the your application that you want to release.

  2. Old version: This will indicate previous version of your application. We will store this in user defaults so that we will know what was the old version when updating our application. This will obviously be nil if your bundle version is 1.0.

  3. Target version: This indicates the version the user is targeting. We will discuss this later.

So, condition such as

bundleVersion > oldVersion or

if(isVersionBetter:myBundleVersion thanVersion:oldVersion)

would either mean we want to create our database (in this case bundle version would be 1.0 and old version will be nil) or update our database (in this case bundle version would be something greater than 1.0 and hence old version would not be nil).

Thus, as we can see, creation of database means user is installing app for the first time. Updating database means user has already installed the app and is updating the database.

But, there might also be a case when you want to update your app and want to keep the database as it is. That is, only UI updating.

Here, target version comes into picture.

As mentioned above, target version is the version the user is targeting. All would work same as above if user is targeting the bundle version. But if user is targeting some other version than bundle version, we would skip database updating part, thus allowing only the UI to change.

So, the final statement would be something like this:

if( bundleVersion == targetVersion AND bundleVersion > oldVersion ) {
// Either create or update the database.
}else {
// Do nothing. Skips database updating and allows UI update.
}

Thus, your database function would look something like this

-(void) initWithTargetVersion:(NSString *) targetVersion {

    NSString *oldDatabaseVersion = [[NSUserDefaults standardUserDefaults] stringForKey:@"OldDatabaseVersion"];
    NSString *bundleDatabaseVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


    if([bundleDatabaseVersion isEqualToString:targetVersion] && [self isVersionBetter:oldDatabaseVersion new:targetVersion]) {
        // Create or update the database.
    }else {
        // Do nothing.
    }
}

where user would pass the target version as follows:

[[DatabaseManager sharedManager] initWithTargetVersion:@"1.0"];
Tuesday, December 6, 2022
 
2

Looks like it's a bug in msbuild or an unsupported feature. Either way I finally found a workaround published on this guy's blog:

http://consultingblogs.emc.com/jamesdawson/archive/2008/06/03/using-linked-files-with-web-application-projects.aspx

Friday, November 11, 2022
 
kallja
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share

Browse Other Code Languages