CURL_MULTI 関数でダウンロードするページ上のリンクを探す PHP スクリプトがあります。ダウンロードは正常に行われ、データは取得されますが、URL が非リンクとしてリストされているページに遭遇すると、スクリプトがランダムにクラッシュします。これはコードです:
$fishnof = strpos($nofresult, $supshorturl, 0);
$return[0] = ''; $return[1] = ''; // always good to cleanset
// Make sure we grabbed a link instead of a text url(no href)
if ($fishnof !== false) {
$linkcheck = rev_strpos($nofresult,'href',$fishnof);
$endthis = false;
while($endthis !== true) {
if($linkcheck > ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
$endthis = true;
break;
}
$lastfishnof = $fishnof;
$fishnof = strpos($nofresult,$supshorturl,$fishnof+1);
if($fishnof === false){$fishnof = $lastfishnof;$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;}// This is the last occurance of our URL on this page
if($linkcheck > $fishnof){$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;} // We went around past the end of the string(probably don't need this)
$linkcheck = rev_strpos($nofresult,'href',$fishnof);
}
if($linkcheck < ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
$return[0] = 'Non-link.';
$return[1] = '-';
$nofresult = NULL; // Clean up our memory
unset($nofresult); // Clean up our memory
return $return;
}
}
これはカスタム rev_strpos で、逆の処理を行うだけstrpos()
です。
// Does a reverse stripos()
function rev_strpos(&$haystack, $needle, $foffset = 0){
$length = strlen($haystack);
$offset = $length - $foffset - 1;
$pos = strpos(strrev($haystack), strrev($needle), $offset);
return ($pos === false)?false:( $length - $pos - strlen($needle) );
}
したがって、次の場合:
$nofresult = '
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
google.com Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
<a href="http://www.google.com">Google</a> Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.';
と
$supshorturl = "google.com";
これにより、HTML の href タグ内にある google.com の 2 番目の出現位置が検出されます。問題は、クラッシュの前にエラーが報告されないことです。私のエラー設定:
ini_set("display_errors", 1);
error_reporting(E_ALL & ~E_NOTICE);
set_error_handler('handle_errors');
私のhandle_errors()
関数はすべてのエラーをファイルに記録します。ただし、スクリプトがクラッシュする前にエラーは報告されません。また、curl_multi は多くの URL を処理し、特定の URL でクラッシュすることもあれば、別の URL でクラッシュすることもあります。午前。もう 1 つの注意点は、while ループを削除してもクラッシュしないことです。また、ページの URL が最初に href タグに含まれていてもクラッシュしません。このことを理解するのを手伝ってください。どうもありがとう!