Viewed   190 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
4

A few years ago I benchmarked the two and CURL was faster. With CURL you create one CURL instance which can be used for every request, and it maps directly to the very fast libcurl library. Using file_get_contents you have the overhead of protocol wrappers and the initialization code getting executed for every single request.

I will dig out my benchmark script and run on PHP 5.3 but I suspect that CURL will still be faster.

Friday, December 16, 2022
 
3

In your url try:

http://user:[email protected]/ 

(append whatever the rest of the URL for your API should be)

Friday, November 4, 2022
 
matoran
 
2

The simplest thing is to do something like this:

NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:<yourNSData>];
[xmlParser setDelegate:self];
[xmlParser parse];

Notice that setDelegate: is setting the delegate to 'self', meaning the current object. So, in that object you need to implement the delegate methods you mention in the question.

so further down in your code, paste in:

    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI
       qualifiedName:(NSString *)qualifiedName 
         attributes:(NSDictionary *)attributeDict{

       NSLog(@"I just found a start tag for %@",elementName);
       if ([elementName isEqualToString:@"employee"]){
       // then the parser has just seen an <employee> opening tag
       }         
     }

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"the parser just found this text in a tag:%@",string);
}

etc. etc.

It's a little harder when you want to do something like set a variable to the value of some tag, but generally it's done using a class variable caleld something like "BOOL inEmployeeTag" which you set to true (YES) in the didStartElement: method and false in the didEndElement: method - and then check for it's value in the foundCharacters method. If it's yes, then you assign the var to the value of string, and if not you don't.

richard

Monday, October 24, 2022
 
4

You can easily use one database from two (or many more) apps. You just need the host name to access the database from anywhere. For example if your database host is whatever.secureserver.net just put that in the connection parameters like so:

$connection = mysql_connect('whatever.secureserver.net', 'username', 'password');

You can do that in two three, four ..etc. different apps accessing the same database. You do however have to make sure that the database allows remote connections if the apps are remote in relation to the database.

If you want to use two databases (db1 and db2 in this example) in one app you can do the following -

If the databases are on the same server:

$connection1 = mysql_connect('whatever.secureserver.net', 'username', 'password');    
$db1_selected = mysql_select_db('db1', $connection1);
$db2_selected = mysql_select_db('db2', $connection1);

If the databases are on different servers:

$connection1 = mysql_connect('whatever.secureserver.net', 'username', 'password');  
$connection2 = mysql_connect('somethingelse.secureserver.net', 'username', 'password');    
$db1_selected = mysql_select_db('db1', $connection1);
$db2_selected = mysql_select_db('db2', $connection2);

There is a more elegant way of handling database connections of course but I chose this verbose answer so it is plainly spelled out for you.

Saturday, December 3, 2022
 
fleur
 
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 :