file_get_contents():stream不支持寻求PHP

问题描述 投票:3回答:3

我正在尝试使用file_get_contents()从伪名称生成器站点(https://fakena.me/fake-name/)中仅获取Name,但是我收到以下警告:

file_get_contents() stream does not support seeking 

我不需要页面的全部内容。我只需要本网站的名称部分:https://fakena.me/fake-name/

我的代码:

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

它在localhost中运行良好,仅在实时网站中显示错误。

php file-get-contents
3个回答
4
投票

你可以在documentation上阅读它:

远程文件不支持寻求(偏移)。尝试寻找非本地文件可能会使用较小的偏移量,但这是不可预测的,因为它适用于缓冲流。

您可以做的是在检索页面内容后使用substr

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

0
投票

这基本上是最新版本中的偏移问题

这里我们需要将$ offset值从-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 )
        {
    }

0
投票

你为什么要抓网站?许多免费API可用于假名称生成器。

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

您将获得JSON格式的输出。使用json_decode()对其进行解码并用于您的目的,与刮擦相比,它也很快。

可选参数

要返回的名称数量,介于1和500之间:

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

将结果限制为男性或女性:

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

特定地区的结果:

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

要求名称中包含最少字符数:

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

要求名称中包含最大字符数:

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

对于JSONP,指定一个回调函数来包装结果:

https://uinames.com/api/?callback=example
© www.soinside.com 2019 - 2024. All rights reserved.