0

I am building an rss feed discovery service by scraping a page URL and finding the <link> tags in the page header. The problem is some URLs take really long to serve the page source so my code gets stuck at file_get_contents($url) very often.

Is there a way to do this with a predefined timeout, for example if 10 seconds have passed and there is still no content served then simply drop that URL and move to the next one?

I was thinking to use the maxLen parameter to get only a part of the source (<head>..</head>) but I'm not sure if this would really stop after the received bytes are reached of would still require the full page load. The other issue with this is that I have no idea what value to set here because every page has different content in the head tag so sizes vary.

4

2 に答える 2

2

私はこれについて読んだばかりなので、これは今のところ理論です..しかし..

これは関数の定義です。リソースコンテキストの部分に注意してください。

string file_get_contents ( string $filename [, bool $use_include_path = false [, **resource $context** [, int $offset = -1 [, int $maxlen ]]]] )

関数の結果を指定し、そのオプション配列でタイムアウト値stream_context_create()を渡すと、機能する可能性があります。

$context = stream_context_create($opts);

または、ストリームを作成してタイムアウトを直接設定することもできます。

http://www.php.net/manual/en/function.stream-set-timeout.php

あなたがそれでいくらかの成功を収めることを願っています。

于 2012-06-17T07:44:32.383 に答える
2

'context'パラメーターを使用します。'stream_context_create'関数を使用し、httpコンテキストで目的のタイムアウトを指定することにより、ストリームコンテキストを作成できます。

$context = stream_context_create(array(
    'http' => array(
        'timeout' => YOUR_TIMEOUT,
    )
));
$content = file_get_contents(SOME_FILE, false, $context);

詳細情報: こことここも。

于 2012-06-17T07:45:15.187 に答える