file get contents - file_get_contents(): stream does not support seeking PHP

711

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.

559

Answer

Solution:

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 usesubstr after what you retrive the contents of the page:

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

Answer

Solution:

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 )
        {
    }
543

Answer

Solution:

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

People are also looking for solutions to the problem: php - How can I search for post's 'title','content'&'meta_query' without WP_Query 's'?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.