tags:

views:

32

answers:

3

i use file_get_contents function to grab data from sites and store the data in database. it will be very inconvenient for me, if one day the script will start not working.

I know, that it can start not working, if they change the structure of site, but now i'm afraid, that maybe there are mechanisms to disable the working of this function, maybe from server?

i tried to find documentation about it, but can't get, so maybe you will help me?

Thanks

+3  A: 

I know, that it can start not working, if they change the structure of site, but now i'm afraid, that maybe there are mechanisms to disable the working of this function, maybe from server?

Yes, it can be disabled from php.ini with allow_url_fopen option. You have other options such as CURL extension too.

Note also that you will need to have openssl extension turned on from php.ini if you are going to use the file_get_contents function to read from a secure protocol.

So in case file_get_contents is/gets disabled, you can go for CURL extension.

Sarfraz
And if you don't want to rely on curl, you can go the manual route with fsockopen. This function is in the core of PHP. There's also the http pecl extension.
Artefacto
@Artefacto: Absolutely, thanks for adding that :)
Sarfraz
+2  A: 

It is possible to disable certain functions using disable_function. Furthermore the support of URLs with filesystem functions like file_get_contents can be disabled with allow_url_fopen. So chances are that file_get_contents might not work as expected one day.

Gumbo
hmmm, too bad:/ and what can i do in this case, to be abble to get the data?
Syom
@Syom: There are special functions for requesting URLs like http://php.net/book.curl
Gumbo
@Syom: Oh, you could also use a stream context with `file_get_contents` (see *context* parameter).
Gumbo
@Gumbo Context? What for?
Artefacto
@Artefacto: You can create a context as a wrapper. See http://php.net/stream_context_create for an example of use.
Gumbo
@Gumbo What I didn't understand was the relevance to the question, but I guess you're just saying how he could things he would otherwise need curl for.
Artefacto
@Artefacto: Yes, using a stream context is just an alternative to when *allow\_url\_fopen* or `file_get_contents` is disabled.
Gumbo
@Gumbo OK, now I'm not following. The stream context just parameterizes the stream that's about to be opened, as far as I know it doesn't allow allow_url_fopen to be circumvented.
Artefacto
@Artefacto: Ah, you’re right. A stream context won’t help if *allow\_url\_fopen* is disabled.
Gumbo
+2  A: 

There are at least two PHP configuration directives that can break your script :

  • If allow_url_fopen is disabled, then, file_get_contents() will not be able to fetch files that are not on the local disk
    • i.e. it will not be able to load remote pages via HTTP.
    • Note : I've seen that option disabled quite a few times
  • And, of course, with disable_functions, any PHP function can be disabled.


Chances are pretty low that file_get_contents() itself will ever get disabled...

But remote-file loading... Well, it might be wise to add an alternative loading mecanism to your script, that would use curl in case allow_url_fopen is disabled.

Pascal MARTIN