Manoj Kumar K
Manoj Kumar K

Reputation: 73

file_get_contents(): stream does not support seeking PHP

I'm trying to use file_get_contents() to grab only Name from fake name generator site(https://fakena.me/fake-name/), however I'm getting the following warning:

file_get_contents() stream does not support seeking 

I dont need entire content of the page. I need only Name section in this site: https://fakena.me/fake-name/.


My code:

$name= file_get_contents('https://fakena.me/fake-name/', NULL, NULL, 849, 32);

Its working well in localhost, showing error only in live website.

Upvotes: 3

Views: 12051

Answers (3)

Pankaj Dadure
Pankaj Dadure

Reputation: 677

Why are you scraping website? Lots of free APIs are available for fake name generator.

TRY:  https://uinames.com/api/

You will get output in JSON format. Decode it using json_decode() and use for your purpose and also it's fast as compared to scraping.

Optional Parameters

The number of names to return, between 1 and 500:

https://uinames.com/api/?amount=25 

Limit results to the male or female gender:

https://uinames.com/api/?gender=female

Region-specific results:

https://uinames.com/api/?region=germany

Require a minimum number of characters in a name:

https://uinames.com/api/?minlen=25

Require a maximum number of characters in a name:

https://uinames.com/api/?maxlen=75

For JSONP, specify a callback function to wrap results in:

https://uinames.com/api/?callback=example

Upvotes: 0

rajwa766
rajwa766

Reputation: 644

This is basically the offset issue in the latest versions

Here we need to change the $offset value from -1 t0 1

  public static function file_get_html( $url, $use_include_path = false, $context = null, $offset = 1, $maxLen = -1, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT )
        {
    }

Upvotes: 0

Federkun
Federkun

Reputation: 36934

You can read about it in the documentation:

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

What you can do is use substr after what you retrive the contents of the page:

$part = substr($name, 849, 32);

Upvotes: 4

Related Questions